1、从终端中输入一串字符,求出空格的个数;ab_cd_e_\n --->遇到\n表示输入结束
---->数组中存储ab_cd_e ----->求出空格个数
2、删除字符串中的空格,要求只用一个数组 “aa_b_cc” --->结果 “aabcc”
3、完成strcmp和strcat函数,再独立完成strlen和strcpy函数
1.
#include<stdio.h>
int main(int argc, const char *argv[])
{
char s[32]={0};
printf("请任意输入一串字符串!\n");
gets(s);
int i=0;
int count=0;
while(s[i]!='\0'){
if(s[i]==' '){
count++;
}
i++;
}
printf("输入的空格的个数为%d\n",count);
return 0;
}
2.
#include<stdio.h>
int main(int argc, const char *argv[])
{
char s[64]={0};
int i=0;
int j=0;
gets(s);
puts(s);
char *p1=s;
char *p2=s;
while(*p2!='\0'){
if(*p2==' '){
p2++;
}else{
*p1=*p2;
p1++;
p2++;
}
}
*p1='\0';
puts(s);
return 0;
}
3.
#include<stdio.h>
int main(int argc, const char *argv[])
{
char s1[32]="hello";
char s2[32]="world";
int i=0,j=0;
int a=0;
while(s1[i]!=s2[j]){
i++;
j++;
}
a=s1[i]-s2[j];
printf("a=%d\n",a);
return 0;
}
4.
#include<stdio.h>
int main(int argc, const char *argv[])
{
int i=0,j=0;
char s1[32]="hello";
char s2[32]=" world!";
puts(s1);
puts(s2);
while(s1[j]!='\0'){
j++;
}
while(s2[i]!='\0'){
s1[j]=s2[i];
i++;
j++;
}
s1[j]='\0';
puts(s1);
return 0;
}
5.
#include<stdio.h>
int main(int argc, const char *argv[])
{
char s1[32]="hello";
char s2[32]="world";
puts(s1);
puts(s2);
int i=0,j=0;
while(s2[i]!='\0'){
s1[i]=s2[i];
i++;
}
s1[i]='\0';
puts(s1);
return 0;
}
6.#include<stdio.h>
#include<string.h>
int main(int argc, const char *argv[])
{
char s[32]="hello";
int i=0;
int len=0;
while(s[i]!='\0'){
i++;
}
printf("%d",i);
putchar(10);
len=strlen(s);
printf("len=%d\n",len);
return 0;
}