两道c语言的期末试题,大家帮帮忙啊,

来源:百度知道 编辑:UC知道 时间:2024/06/23 03:39:59
1. 设计程序,输入一个字符串,通过调用一个有返回值的函数int count(char *s),统计字符串中出现空白字符(即空格,\t,\n)的次数。例如:字符串“a b c”中空白字符有两个,则函数返回值为2
2. 设计程序,输入两个字符串,通过调用自编函数char *copy(char *s1,char s2),实现将字符串s2中的所有非空白字符复制到s1中。函数返回值是字符串s1的起始地址。例如:若s2指向字符串“a b c abc”,复制后,则s1指向字符串“abcabc”。
回答后一定给足够的悬赏分

第一题:
#include<stdio.h>
int count(char *s)
{ int i,n=0;
for(i=0;s[i]!='\0';i++)
if(s[i]==32||s[i]=='\t'||s[i]=='\n') n++;
return n;
}
int main(void)
{ char s[50];
gets(s);
printf("%d\n",count(s));
}
第二题:
#include<stdio.h>
char *copy(char *s1,char *s2)
{ int i,j;
for(i=0,j=0;s2[i]!='\0';i++)
if(s2[i]!=32&&s2[i]!='\t'&&s2[i]!='\n') s1[j++]=s2[i];
s1[j]='\0';
return s1;
}
int main(void)
{ char s1[50],s2[50];
gets(s2);
copy(s1,s2);
puts(s1);
printf("\n");
}