1、从终端中输入一串字符,求出空格的个数;ab_cd_e_\n --->遇到\n表示输入结束
---->数组中存储ab_cd_e ----->求出空格个数
#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、删除字符串中的空格,要求只用一个数组 “aa_b_cc” --->结果 “aabcc”
#include<stdio.h>
int main(int argc, const char *argv[])
{
char s[64]={0};
gets(s);
puts(s);
int i=0;
int j=0;
while(s[j]!='\0'){
if(s[j]==' '){
j++;
}else{
s[i]=s[j];
i++;
j++;
}
}
s[i]='\0';
puts(s);
return 0;
}
3、完成strcmp和strcat函数,再独立完成strlen和strcpy函数
#include <stdio.h>
int main(int argc, const char *argv[])
{
char s[100]="hqyj";
printf("sizeof(s)=%ld\n",sizeof(s));
//统计有效字符的个数
int count = 0;
int i = 0;
//while(si!='\0')这样写也可以
while(s[i])
{
count++;
i++;
}
printf("count = %d\n",count);
return 0;
}
#include <stdio.h>
int main(int argc, const char *argv[])
{
char s1[32]="zhangsan";
char s2[32]="lisi";
//你的操作,将s2赋值给s1
int i = 0;
while(s2[i]!='\0')
{
s1[i]=s2[i];
i++;
}
s1[i]='\0';
printf("s1 = [%s]\n",s1);
return 0;
}
#include <stdio.h>
int main(int argc, const char *argv[])
{
char s1[32]="aabbcc";
char s2[32]="112233";
//定位s1的'\0'
int i = 0;
int j = 0;
while(s1[i]!='\0')
{
i++;
}
//当循环结束时,i保存的就是s1中'\0'的下标志
//执行追加操作
while(s2[j]!='\0')
{
s1[i]=s2[j];
j++;
i++;
}
//将s2的'\0'追加到s1的后面
s1[i]=s2[j];
//结尾加上'\0'
printf("s1 = [%s]\n",s1);
return 0;
}