#include <stdio.h>
#define N 50
int main(){char a[50];int num = 0,flag;printf("请输入:");gets(a);puts(a);for(int i = 0;i < N - 1;i++){if(a[i] == ' '){num++;}if(a[i] == '\0'){flag = i;break;}};printf("空格个数:%d\n",num);for(int i = 0;i <= flag;i++){if(a[i] == ' '){ for(int j = i;j <= flag;j++){a[j] = a[j+1];}i--;}if(a[i] == '\0' && i > -1){break;}}printf("删除空格后:");puts(a);printf("\n");return 0;
}
-
strcmp
#include <stdio.h>int main(){char a[50],b[50];int i = 0;printf("请输入第一个字符串:");gets(a);printf("请输入第二个字符串:");gets(b);puts(a);puts(b);while(a[i] - b[i] == 0){if(a[i] == '\0'){printf("相同\n");return 0;}i++;}printf("%d\n",a[i] - b[i]);printf("\n");return 0;
}
-
strcat
#include <stdio.h>
int main(){char a[50],b[50];int i = 0,j = 0;printf("请输入第一个字符串:");gets(a);printf("请输入第二个字符串:");gets(b);puts(a);puts(b);while(a[i] != '\0' ){i++;}while(b[j] != '\0'){a[i] = b[j];i++;j++;}a[i] = b[j];puts(a);printf("\0");return 0;
}
-
strcpy
#include <stdio.h>
int main(){char a[] = "hello world";char b[50];int i = 0;while(a[i] != '\0'){b[i] = a[i];i++;}b[i] = a[i];puts(b);printf("\n");return 0;
}
-
strlen
#include <stdio.h>
int main(){char a[50];int len = 0,i = 0;gets(a);while( a[i] != '\0' ){len++;i++;}printf("长度: %d\n",len);return 0;
}