思路:定义一个插入的位置p,把p后面的字符串全部右移一位,空出来的那个位置给要插入的字符
1 #include <stdio.h> 2 #include <string.h> 3 4 int main() 5 { 6 int i,a,b,j,p; 7 char str[32]={0}; 8 char ch; 9 printf("zifuchuan weizhi zifuchuan\n"); 10 scanf("%s%d %c",str,&p,&ch); 11 str[p-1]=ch; 12 a=strlen(str); 13 for(i=0;i<a-p+1;i++) 14 { 15 str[a-i+1]=str[a-i]; 16 } 17 printf("%s\n",str); 18 return 0; 19 }思路:定义一个新的字符串tim,定义一个要插入的位置p,把p前面的字符串赋值给tim,然后再用连接函数strcat,把要插入的字符连在tim的后面,定义插入的字符串ch的长度为b,原来的字符串str长度为a,把str全部左移p个位置,最后再把他连接在字符串tim后面,注意要用strncat去连接,因为要连接的是之前剩下来的,所以长度减小了。
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int p,i; 6 char tim[32] = {0}; 7 char str[32] = {0}; 8 char ch[32] = {0}; 9 printf("zifuchuan zifu zifuchuan\n"); 10 scanf("%s%d%s",str,&p,ch); 11 int a = strlen(str); 12 int b = strlen(ch); 13 for(i = 0; i < p - 1; i++) 14 { 15 tim[i] = str[i]; 16 } 17 strcat(tim,ch); 18 for(i = 0; i < a - p + 1; i++) 19 { 20 str[i] = str[p+i-1]; 21 } 22 strncat(tim,str,a-p+1); 23 printf("%s\n",tim); 24 return 0; 25 }思路,在一个数组内,从第一个数组开始,一个个往后面的数比较,前面的大于后面的数就交换,这样就把最大的数找出来了并排在了最后面,之后循环次数减一,继续找出第二个大的数等。
1 #include <stdio.h> 2 3 int main() 4 { 5 int i,j; 6 int a[5]; 7 printf("qing shu ru 5 ge shu zi :\n"); 8 for (i = 0; i < 5; i++) 9 { 10 scanf("%d",&a[i]); 11 } 12 for(i = 0; i < sizeof(a) / sizeof(a[0])-1; i++) 13 { 14 for(j = 0; j < sizeof(a) / sizeof (a[0])-i-1; j++) 15 { 16 if(a[j] > a[j+1]) 17 { 18 int temp = a[j]; 19 a[j] = a[j+1]; 20 a[j+1] = temp; 21 } 22 } 23 } 24 for(i = 0; i < 5; i++) 25 { 26 printf("%d ",a[i]); 27 } 28 printf("\n"); 29 return 0; 30 }思路:先定义一个数组str,然后用strlen函数求出他的长度,然后通过循环,让他第一个元素赋值给最后一个,第二个元素赋值给倒数第二个,注意的是每次赋值,str里面的数组都是改变的,所以当第longth/2个元素开始,str里面的元素就是已经被赋值改变了的,所以在这之前要定义一个新的数组tim,让他等于str,这样每次循环tim都不会变。
1 #include <stdio.h> 2 #include <string.h> 3 int main() 4 { 5 int i,longth; 6 char str[32] = {0}; 7 char tim[32] = {0}; 8 printf("qing shu ru yi ge zifuchuan : \n"); 9 scanf("%s",str); 10 longth = strlen(str); 11 for( i = 0; i < longth; i++) 12 { 13 tim[i] = str[i]; 14 } 15 for( i = 0; i < longth ; i++) 16 { 17 str[i] = tim[longth - 1 - i]; 18 } 19 printf("%s\n",str); 20 return 0; 21 }思路:本题的切入点是求出在字符串中head中h的位置以及tail中l的位置,利用数组,通过循环求出这两个位置,再通过一个循环把这两个位置中的字符都打印出来,就是我们要求的字符串。
1 #include <stdio.h> 2 #include <string.h> 4 int main() 5 { 6 char str[32] = {0}; //定义原字符串 7 char head[32] = "head";//定义一个字符串head 8 char tail[32] = "tail";//定义一个字符串tail 9 char tou[32] = {0};//定义两个空字符串tou和wei 10 char wei[32] = {0}; 11 int i,j,k,p1,p2,str_longth,tail_longth,head_longth;//定义循环变量i,j,k; head开头位置p1和tail结尾位置p2,以及str,tail,head的长度; 12 printf("qing shu ru\n");//提升请输入 13 scanf("%s",str);//输入 14 str_longth = strlen(str);//求出三个字符串的长度 15 head_longth = strlen(head); 16 tail_longth = strlen(tail); 17 for(i = 0; i < str_longth - head_longth + 1; i++)//求出p并打印出来 18 { 19 for(j = 0; j < head_longth; j++) 20 tou[j] = str[i + j]; 21 if(strcmp(tou,head) == 0) 22 { 23 p1 = i + 1; 24 printf("p1 = %d\n",i+1); 25 break; 26 } 27 } 28 for(i = 0; i < str_longth - tail_longth +1; i++)//同样的方法求出p2并打印出来 29 { 30 for(j = 0; j < tail_longth; j++) 31 wei[j] = str[i + j]; 32 if(strcmp(wei,tail) == 0) 33 { 34 p2 = i + 4; 35 printf("p2 = %d\n",i+4); 36 break; 37 } 38 } 39 for(k = p1 - 1; k < p2; k++)//把要求的字符串打印出来 40 { 41 printf("%c",str[k]); 42 } 43 printf("\n"); 44 return 0; 45 }思路:先定义一个数组形式的字符串str,用strlen函数算出他的长度,长度作为循环次数,在循环里面挨个判断这个数组的每一项是否等于这个字符,如果相等,则跳出循环,此时循环的第几次就是位置,最后输出这个位置。
1 #include <stdio.h> 2 #include <string.h> 3 4 int main() 5 { 6 char str[32] = {0}; 7 char a; 8 int i,p,longth; 9 printf("请输入字符串,和需要查找位置的字符\n"); 10 scanf("%s %c",str,&a); 11 longth = strlen(str); 12 for(i = 0; i < longth; i++) 13 { 14 if 15 { 16 p = i+1; 17 break; 18 } 19 } 20 printf("%d\n",p); 21 return 0; 22 }思路:定义三个数组形式的字符串,一个是原字符串str,一个是要查找的字符串tim,还有一个空字符串ch,str和tim通过scanf函数输入得到,用strlen函数分别算出str和tim的长度,利用嵌套循环,每次循环把和tim字符串相同的长度的字符串赋值给空字符串ch,然后在循环里面,如果ch等于tim,则ch就是我们要寻找的字符串,他的位置就是当前循环的次数。
1 #include <stdio.h> 2 #include <string.h> 3 4 int main() 5 { 6 char str[32] = {0}; 7 char tim[32] = {0}; 8 char ch[32] = {0}; 9 int i,j,p,k,str_longth,tim_longth; 11 printf("请输入字符串,和需要查找位置的字符串\n"); 12 scanf("%s%s",str,tim); 13 str_longth = strlen(str); 14 tim_longth = strlen(tim); 15 for(i = 0; i < (str_longth - tim_longth +1); i++) 16 { 17 for(j = 0; j < tim_longth; j++) 18 ch[j] = str[i+j]; 19 if(strcmp(ch,tim) == 0) 20 { 21 p = i + 1; 22 printf("%d\n",p); 23 break; 24 } 25 } 26 return 0; 27 }