C Primer Plus(第五版)第11章 字符串和字符串函数

11.1
#include <stdio.h>
#include<string.h>
char * storage(char *,int);
int main(void)
{
char ch[100];
fputs(storage(ch,10),stdout);
return 0;
}
char * storage(char * ch,int n)
{
int i;
for(i=0;i<n;i++)
ch[i]=getchar();
ch[i]=0;
return ch;
}

11.2
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
char * storage(char *,int );
int main(void)
{
char ch[100];
int i;
puts("plesae,input the number of the most characters.");
i=atoi(fgets(ch,100,stdin));
puts("now,input the characters.");
fputs(storage(ch,i),stdout);
return 0;
}
char * storage(char * ch,int n)
{
int i;
char c;
for(i=0;i<n;i++)
{
if(isblank(c=getchar()))
{
ch[i]=0;
return ch;
}
else ch[i]=c;
}
ch[i]=0;
return ch;
}

11.3
#include<stdio.h>
char *read(char*);
int main(void)
{
char ch[100];
puts("please,the string!");
puts(read(ch));
return 0;
}
char *read(char*ch)
{
scanf("%s",ch);
while(getchar()!='\n')
;
return ch;
}

11.4
#include<stdio.h>
char *search(char*,char);
int main(void)
{
char str[100];
char c;
char *p;
puts("input a string that you want to search a characet in it.");
fgets(str,100,stdin);
puts("input a character that you want to search.");
while(scanf(" %c",&c)!=EOF)
{
if ((p=search(str,c))==0)
printf("sorry, there is not the %c character.\n",c);
else
printf("the location is %p.\n",p);
puts("input a character,again!");
}
return 0;
}
char *search(char *str,char c)
{
while(*str!=0)
{
if (*str==c)
return str;
else str++;
}
return NULL;
}

11.5
#include<stdio.h>
int is_within(char*,char);
int main(void)
{
char str[100];
char c;
puts("input a string that you want to search a characet in it.");
fgets(str,100,stdin);
puts("input a character that you want to search.");
while(scanf(" %c",&c)!=EOF)
{
if (is_within(str,c))
printf("yes,there is the character %c.\n",c);
else
printf("no,there is not the character %c.\n",c);
puts("input a character,again!");
}
return 0;
}
int is_within(char *str,char c)
{
while(*str!=0)
{
if (*str==c)
return 1;
else str++;
}
return 0;
}

11.6
#include<stdio.h>
#include<string.h>
char *strncpys(char*,char*,int);
int main(void)
{
char s1[100],s2[100];
int n;
puts("how many characters you want to copy.");
scanf("%d",&n);
while(getchar()!='\n')
;
puts("input a string for copy.");
while(fgets(s2,100,stdin))
{
puts(strncpys(s1,s2,n));
puts("input a string for copy.");
}
return 0;

}
char *strncpys(char*s1,char*s2,int n)
{
int i,j;
j=strlen(s2)-1;
if (j<n)
{
for(i=0;i<j;i++)
s1[i]=s2[i];
while(i<n)
{
s1[i]=0;
i++;
}
}
else
{
for(i=0;i<n;i++)
s1[i]=s2[i];
s1[i]=0;
}
return s1;
}

11.7
#include<stdio.h>
#include<string.h>
char *string_in(char*,char*);
int main(void)
{
char s1[100],s2[100];
puts("input a string and a substring.");
while(fgets(s1,100,stdin)&&fgets(s2,100,stdin))
{
if(string_in(s1,s2))
puts("yes,the substring is in the first string");
else
puts("no,the substring is not in the first string");
puts("input a string and substring again.");
}
return 0;
}
char *string_in(char*s1,char*s2)
{
int i,j,k;
i=strlen(s1);
j=strlen(s2);
for(k=0;k<=i-j;k++)
if(strncmp(s1+k,s2,j-1)==0)
return s1;
return NULL;
}

11.8
#include<stdio.h>
#include<string.h>
char* reverse(char *);
int main(void)
{
char s[100];
puts("input a string for reverse.");
while(fgets(s,100,stdin))
{
puts(reverse(s));
puts("input a string for reverse,again.");
}
return 0;
}
char * reverse(char*s)
{
int i,j,k;
j=strlen(s)-1;
k=j;
char temp[j+1];
for(i=0;i<j;i++)
temp[--k]=s[i];
temp[j]=0;
return strcpy(s,temp);
}

11.9
#include<stdio.h>
#include<string.h>
char* delete(char *);
int main(void)
{
char s[100];
puts("input a string for delete blank.");
while(fgets(s,100,stdin))
{
puts(delete(s));
puts("input a string for delete blank,again.");
}
return 0;
}
char * delete(char*s)
{
int i,j;
i=strlen(s);
for(j=0;j<i;j++)
if (s[j]==' ')
{
s[j]=0;
strcat(s,s+j+1);
j--;
}
return s;
}

11.10
#include<stdio.h>
#include<string.h>
void output(char**,int);
char **ascii(char**,int);
char** LONG(char**,int);
char**first(char**,int);
int count(char*);
int main(void)
{
char s[10][100]={0};
char *p[10];
char c;
int i;
for(i=0;i<10;i++)
p[i]=s[i];
i=0;
puts("input ten strings,EOF for quit.");
while(fgets(p[i++],100,stdin)&&i<10)
puts("input a string,EOF for quit.");
puts("**********************************************");
puts("1) output the strings 2) output in ascii order");
puts("3) output according the length 4) output according the first word length");
puts("5) quit");
puts("**********************************************");
while((c=getchar())!='5')
{
switch (c)
{
case '1':
output(p,i);
break;
case '2':
output(ascii(p,i),i);
break;
case '3':
output(LONG(p,i),i);
break;
case '4':
output(first(p,i),i);
break;
default:
break;
}
while(getchar()!='\n')
;
puts("**********************************************");
puts("1) output the strings 2) output in ascii order");
puts("3) output according the length 4) output according the first word length");
puts("5) quit");
puts("**********************************************");
}
return 0;
}
void output(char **p,int i)
{
int j;
for(j=0;j<i;j++)
puts(p[j]);
}
char ** ascii(char**p,int i)
{
int j,k;
char *temp;
for(j=0;j<i-1;j++)
for(k=j;k<i-1;k++)
if (strcmp(p[j],p[k+1])>0)
{
temp=p[j];
p[j]=p[k+1];
p[k+1]=temp;
}
return p;
}
char** LONG(char**p,int l)
{
int i,j;
char * temp;
for(i=0;i<l-1;i++)
for(j=i;j<l-1;j++)
if(strlen(p[i])>strlen(p[j+1]))
{
temp=p[i];
p[i]=p[j+1];
p[j+1]=temp;
}
return p;
}
char** first(char**p,int i)
{
int j,k;
char *temp;
for(j=0;j<i-1;j++)
for(k=j;k<i-1;k++)
if(count(p[j])>count(p[k+1]))
{
temp=p[j];
p[j]=p[k+1];
p[k+1]=temp;
}
return p;
}
int count(char *s)
{
int i;
for(i=0;s[i]!=' '&&s[i]!=0;i++)
;
return i;
}

11.11
#include<stdio.h>
#include<ctype.h>
int main(void)
{
char c;
int upper,lower,punct,num;
upper=lower=punct=num=0;
while((c=getchar())!=EOF)
if(isupper(c))
upper++;
else if(islower(c))
lower++;
else if(ispunct(c))
punct++;
else if(isdigit(c))
num++;
printf("there are %d upper characters,"
"%d lower charaters,"
"%d puncts,"
"%d digit.",upper,lower,punct,num);
}

11.12
#include<stdio.h>
int main(int num,char**p)
{
int i,j;
j=num;
for(i=0;i<j-1;i++)
{
fputs(p[--num],stdout);
fputs(" ",stdout);
}
puts("\0");
return 0;
}

11.13
#include<stdio.h>
#include<stdlib.h>
double main(int num,char**p)
{
int i,j;
double f,sum;
i=atoi(p[2]);
f=atof(p[1]);
sum=1;
for(j=0;j<i;j++)
sum*=f;
printf("the sum is %f.\n",sum);
return sum;
}

11.4
#include<stdio.h>
#include<ctype.h>
double ndimatial(int);
int atoiok(char *);
int main(void)
{
char s[100];
fgets(s,100,stdin);
printf("%d\n",atoiok(s));
return 0;
}
int atoiok(char * c)
{
int i,j,k;
double sum=0;
for(i=0;c[i]!=0&&isdigit(c[i]);i++)
;
k=i-1;
for(j=0;j<i;j++)
sum+=(c[j]-48)*ndimatial(k--);
return (int)sum;

}
double ndimatial(int n)
{
double sum=1;
int i;
for(i=0;i<n;i++)
sum*=10;
return sum;
}

11.5
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main(int num,char** p)
{
int i;
int c[1000000];
for(i=0;c[i]=getchar();i++){
if(c[i]==EOF)
break;}
if(!strncmp(p[1],"-p",2))
for(i=0;c[i]!=EOF;i++)
putchar(c[i]);
else if(!strncmp(p[1],"-u",2))
for(i=0;c[i]!=EOF;i++)
putchar(toupper(c[i]));
else if(!strncmp(p[1],"-l",2))
for(i=0;c[i]!=EOF;i++)
putchar(tolower(c[i]));
return 0;
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值