1 #include<stdio.h>
2 #include<string.h>
3 int main(int argc, const char *argv[])
4 {
5 char a[]="hello my student";
6 int n=strlen(a);
7 char b[20]="";
8 int s=0;char t;
9 for(int i=n-1;i>=0;i--){
10 b[s]=a[i];s++;
11 }
12 int i=0,j=0;
13 while(b[i]!='\0'){
14 int q=0;
15 while(b[j]!=' '&&b[j]!='\0')
16 {j++;}
17 q=j-1;
18 while(i<q){
19 t=b[i];b[i]=b[q];b[q]=t;
20 i++;q--;}
21 while(b[j]==' ')
22 {j++;}
23 i=j;
24 }
25 printf("结果是:%s\n",b);
26 return 0;
27 }
![]()
该代码段展示了一个C语言程序,它首先定义了一个字符串a,然后计算其长度并创建一个空字符串b。程序从a的末尾开始,逐字符复制到b中,实现字符串反转。之后,程序遍历反转后的字符串b,找到并替换所有空格,最后打印出结果。
1519

被折叠的 条评论
为什么被折叠?



