C语言程序设计课后习题答案(第四版)谭浩强 下载本文

void inverse(char str[]) {char t; int i,j;

for (i=0,j=strlen(str);i<(strlen(str)/2);i++,j--) {t=str[i];

str[i]=str[j-1]; str[j-1]=t; } } 7-6

#include <> int main()

{void concatenate(char string1[],char string2[],char string[]); char s1[100],s2[100],s[100]; printf(\ scanf(\

printf(\ scanf(\

concatenate(s1,s2,s);

printf(\ return 0; }

void concatenate(char string1[],char string2[],char string[]) {int i,j;

for (i=0;string1[i]!='\\0';i++) string[i]=string1[i];

for(j=0;string2[j]!='\\0';j++) string[i+j]=string2[j]; string[i+j]='\\0'; } 7-7

#include <> int main()

{void cpy(char [],char []); char str[80],c[80]; printf(\ gets(str);

cpy(str,c);

printf(\ return 0; }

void cpy(char s[],char c[]) { int i,j;

for (i=0,j=0;s[i]!='\\0';i++)

if (s[i]=='a'||s[i]=='A'||s[i]=='e'||s[i]=='E'||s[i]=='i'|| s[i]=='I'||s[i]=='o'||s[i]=='O'||s[i]=='u'||s[i]=='U') {c[j]=s[i]; j++; } c[j]='\\0'; } 7-8

#include <> #include <> int main() {char str[80];

void insert(char []);

printf(\ scanf(\ insert(str); return 0; }

void insert(char str[]) {int i;

for (i=strlen(str);i>0;i--) {str[2*i]=str[i]; str[2*i-1]=' '; }

printf(\} 7-9

#include <>

int letter,digit,space,others; int main()

{void count(char []); char text[80];

printf(\ gets(text);

printf(\ puts(text); letter=0; digit=0; space=0; others=0; count(text);

printf(\ return 0; }

void count(char str[]) {int i;

for (i=0;str[i]!='\\0';i++)

if ((str[i]>='a'&& str[i]<='z')||(str[i]>='A' && str[i]<='Z')) letter++;

else if (str[i]>='0' && str [i]<='9') digit++;

else if (str[i]==32) space++; else

others++; } 7-10

#include <> #include <> int main()

{int alphabetic(char); int longest(char []); int i;

char line[100];

printf(\ gets(line);

printf(\

for (i=longest(line);alphabetic(line[i]);i++) printf(\ printf(\ return 0; }

int alphabetic(char c)

{if ((c>='a' && c<='z')||(c>='A'&&c<='z')) return(1); else

return(0); }

int longest(char string[])

{int len=0,i,length=0,flag=1,place=0,point; for (i=0;i<=strlen(string);i++) if (alphabetic(string[i])) if (flag) {point=i; flag=0; } else

len++; else

{flag=1;

if (len>=length) {length=len; place=point; len=0; } }

return(place); } 7-11

#include <> #include <> #define N 10 char str[N]; int main()

{void sort(char []); int i,flag;

for (flag=1;flag==1;)

{printf(\ scanf(\ if (strlen(str)>N)

printf(\ else

flag=0; }