字符串
#include <stdio.h> #include <string.h> int main() { char str1[]="abcdefghijklmnopqretuvwxyz"; char str2[]="iiiii"; char str3[]="abcdefghijklmnopqretuvwxyz"; char str4[]="oooooooo"; int count1,count2; count1=0; while (str1[count1]) count1++; printf("\n%d\n",count1); count2=strlen(str2); printf("%d\n",count2); if(sizeof (str2)<=sizeof(str1)) strcpy(str1,str2); printf("%s",str2); printf("\n\n%s",str1); count2=0; count1=8; while (str3[count2]) str4[count1++]=str3[count2++]; printf("\n\n\n\n\n%s",str4); system("pause"); }
#include <stdio.h> int main() { char a[]="To be or not to be"; char b[]=",that is a question"; int count =0; while (a[count]!='\0') count++; printf("\n The length of the string \"%s\" is %d characters.\n",a,count); count=0; while (b[count]) count++; printf("\n The length of the string \"%s\" is %d characters.",b,count); system("pause"); return 0; }
#include <stdio.h> int main() { char a[44]="To be or not to be"; char b[]=",that is a question"; int count1 =0; int count2 =0; while (a[count1]!='\0') count1++; printf("\n The length of the string \"%s\" is %d characters.\n",a,count1); while (b[count2]) count2++; printf("\n The length of the string \"%s\" is %d characters.",b,count2); if(sizeof a<count1+count2+1) printf("\n You can't do it "); else { count2=0; while (b[count2]) a[count1++]=b[count2++]; a[count1]='\0'; printf("\n\n%s",a); } system("pause"); return 0; }//连接字符串
#include <stdio.h> int main() { int i; char str[][40]={ "To be or not to be",//18 "that is a problem."//19 };//二维数组声明 int count[]={0,0}; for(i=0;i<2;i++) while((str[i][count[i]])!=0)//!=0我加的 count[i]++; /* count[0]=0; i=0 str[0][0]!=0 count[0]++ str[0][1]!=0 count[0]++ ... ... str[0][18]!=0 count[0]==18 str[0][19]=0 跳出while count[1]=0; i=1; str[1][count[1]]!=0 count[1]++ ... ... str[1][18]!=0 count[1]++ 此时=19 跳出while */ if(sizeof str[0]<count[0]+count[1]+1)//count[0],count[1]分别为字符个数(不包括\0) printf("\n You can't put a quart into a pin pot."); else { count[1]=0;//正好接到后面 while (( str[0][count[0]++]=str[1][count[1]++] )); printf("\n%s\n",str[0]); } system("pause"); return 0; }//字符串数组
#include <stdio.h> #include <string.h> #define S 40 int main(void) { char str1[S]="abcdefghijklmnopqretuvwxyz"; char str2[S]="iiiiiiiiiiii"; char str3[]="The quick brown fox"; char str4[]="The quick black fox"; if (S>strlen(str1)+strlen(str2)) printf("\n %s \n",strcat(str1,str2)); else printf("\n You can't put a quart int a pint pot"); if(strcmp(str3,str4)>0) printf("str3>str4"); else printf("str3<str4"); system("pause"); return 0; }
#include <stdio.h> #include <string.h> #define S 40 int main(void) { char str1[]="The quick brown fox"; char str2[]="The quick black fox"; char str3[]="The quick"; if(strstr(str2,str3)==NULL) printf("shibai"); else printf("zhaodaole"); system("pause"); return 0; }
#include <stdio.h> #include <string.h> #include <ctype.h> #define S 20 int main(void) { char buffer[S]; int i=0; int num_letters =0; int num_digits =0; printf("\n Enter an interesting string of less than 80 ..."); gets(buffer); if(gets(buffer)==NULL) { printf("Error input"); return 1; }//检验gets函数是否出错(一般读取文件可能错),一般键盘输入不会出错(gets函数可以接受所有的字符包括空格 不像scanf那样遇见空格视为结束) while (buffer[i]!='\0') { if(isalpha(buffer[i])) num_letters++; if(isdigit(buffer[i++])) num_digits++; } printf("\nzhimu:%d\nshuzhi:%d\n",num_letters,num_digits); system("pause"); return 0; }