C语言程序设计之字符串篇2
创始人
2024-11-12 06:11:08
0

程序设计之字符串2

  • 字符串
  • 问题2_1
    • 代码2_1
    • 结果2_1
  • 问题2_2
    • 代码2_2
    • 结果2_2
  • 问题2_3
    • 代码2_3
    • 结果2_3
  • 问题2_4
    • 代码2_4
    • 结果2_4
  • 问题2_5
    • 代码2_5
    • 结果2_5
  • 问题2_6
    • 代码2_6
    • 结果2_6

字符串

        在字符串操作后切记最后得加上字符串结束标识符 ′ \ 0 ′ '\backslash0' ′\0′ 。
        用数组作为函数的形参时,可以不定义数组的行数,但一定要定义数组的列数。

问题2_1

        函数 f u n fun fun 的功能是:删除指针 p p p 所指字符串中的所有空白字符(包括制表符、回车符及换行符)。输入字符串时用 # 号结束输入。

代码2_1

#include #include #include  void fun(char *p){ 	int i, t; 	char c[80]; 	 	for(i=0,t=0; p[i]; i++){ 		if(!isspace(*(p+1))) 			c[t++] = p[i]; 	} 	c[t] = '\0'; 	strcpy(p, c); }   void main(void){ 	char c, s[80]; 	int i=0; 	printf("Input a string:\n"); 	c = getchar(); 	while(c!='#'){ 		s[i] = c; 		i++; 		c = getchar(); 	} 	s[i] = '\0'; 	fun(s); 	printf("\nThe result:\n"); 	puts(s); } 

结果2_1

Result_2_1

问题2_2

         函数 f u n fun fun的功能是:求形参 s s s 所指字符串中最后一次出现的 t t t 所指字符串的地址,并通过函数值返回,在主函数中输出从此地址开始的字符串;若未找到,则函数值为 NULL。
        例如: 形参 s s s 所指的字符串为 “ a b c d a b f a a b c d e ” “abcdabfaabcde” “abcdabfaabcde” , t t t 中的字符为 a b c d abcd abcd ,执行后字符串应为 " a b c d e " "abcde" "abcde" 。当 t t t 中的字符为 g h f ghf ghf ,则程序输出未找到信息 " n o t b e f o u n d ! " "not be found!" "notbefound!" 。

代码2_2

#include #include #include #include  char *fun(char *s, char *t){ 	char *p, *r, *a; 	a = NULL; 	while(*s){ 		p = s; 		r = t; 		while(*r){ 			if(*r==*p){ 				r++; 				p++; 			} 			else 				break; 		} 		if(*r=='\0') 			a = s; 		s++; 	} 	return a; }  void main(void){ 	char s[100], t[100], *p; 	system("CLS"); 	printf("\nPlease enter string s: "); 	scanf("%s", s); 	printf("\nPlease enter substring t: "); 	scanf("%s", t); 	p = fun(s, t); 	if(p) 		printf("\nThe result is: %s\n", p); 	else 		printf("\nNot found!\n"); } 

结果2_2

Result_2_2

问题2_3

         函数 f u n fun fun的功能是:在形参 s s s 所指字符串中找出 t t t 所指字符串的个数作为函数值返回。
        例如: 形参 s s s 所指的字符串为 “ a b c d a b f a b ” “abcdabfab” “abcdabfab” , t t t 所指字符串的内容为 a b ab ab ,则函数返回值为 3 3 3 。

代码2_3

#include #include #include #include  int fun(char *s, char *t){ 	int n; 	char *p, *r; 	while(*s){ 		p =s; 		r = t; 		while(*r){ 			if(*r==*p){ 				r++; 				p++; 			} 			else 				break; 		} 		if(*r=='\0') 			n++; 		s++; 	} 	return n; }  void main(void){ 	char s[100], t[100]; 	int m; 	system("CLS"); 	printf("\nPlease enter string s : "); 	scanf("%s", s); 	printf("\nPlease enter substing t : "); 	scanf("%s", t); 	m = fun(s, t); 	printf("\nThe result is : m = %d \n", m); } 

结果2_3

Resul_2_3

问题2_4

         函数 f u n fun fun的功能是:将一个数字字符串转换为一个整数(不得调用C语言提供的将字符串转换为整数的函数)。
        例如: 若输入字符串为 " − 1234 " "-1234" "−1234" 则函数把它转换为整数值 -1234$ 。

代码2_4

#include #include  long fun(char *p){ 	long n=0; 	int flag=1; 	if(*p=='-'){ 		p++; 		flag = -1; 	} 	else if(*p=='+') 		p++; 	while(*p!='\0'){ 		n = n*10+*p-'0'; 		p++; 	} 	return n*flag; }   void main(void){ 	char s[6]; 	long n; 	printf("Enter a string : \n"); 	gets(s); 	n = fun(s); 	printf("\n%ld\n", n); } 

结果2_4

Result_2_4

问题2_5

         函数 f u n fun fun的功能是:判断 c h ch ch 中的字符是否与 s t r str str 所指串中的某个字符相同;若相同,什么也不做,若不同,则将其插在串的最后 。

代码2_5

#include #include  void fun(char *str, char ch){ 	while(*str&&*str!=ch) 		str++; 	if(*str!=ch){       // 思考这里为什么是最后??? 		str[0] = ch; 		str[1] = 0; 	} }  void main(){ 	char s[81], c; 	printf("\nPlease enter a string:\n"); 	gets(s); 	printf("Please enter the character to search : "); 	c = getchar(); 	fun(s, c); 	printf("\nThe result is %s\n", s); } 

结果2_5

Result_2_5

问题2_6

         函数 f u n fun fun的功能是:统计一个长度为 2 的字符串在另一个字符串中出现的次数。
        例如: 输入字符串为 " a s d   h g a s   g h   a s   6 d a s " "asd\ hgas\ gh\ as\ 6das" "asd hgas gh as 6das" ,字符串为 " a s " "as" "as" ,则应当输出 4 。

代码2_6

#include #include #include #include  int fun(char *str, char *substr){ 	int i, j; 	for(i=0; str[i+1]!='\0'; i++){ 		if(str[i]==substr[0]&&str[i+1]==substr[1]) 			j++; 	} 	return j; }  void main(void){ 	char str[81], substr[3]; 	int n; 	system("CLS"); 	printf("输入主字符串 : "); 	gets(str); 	printf("输入子字符串 : "); 	gets(substr); 	puts(str); 	puts(substr); 	n = fun(str, substr); 	printf("\n = %d\n", n); } 

结果2_6

Result_2_6

相关内容

热门资讯

技巧辅助挂!pokermast... 技巧辅助挂!pokermaster修改器,丹东约战麻将辅助器,演示教程(有挂细节)1、点击下载安装,...
现场直击!wepokerplu... 现场直击!wepokerplus万能挂,丰城双剑新版最强高分攻略,操作教程(有挂方针)1.丰城双剑新...
插件辅助挂!wepoker有辅... 插件辅助挂!wepoker有辅助器吗,乐平包王攻略,学习教程(有挂方略)1、首先打开乐平包王攻略辅助...
据玩家消息!拱趴大菠萝辅助神器... 据玩家消息!拱趴大菠萝辅助神器,多乐跑得快辅助器,机巧教程(证实有挂)1、在拱趴大菠萝辅助神器插件功...
此事备受玩家关注!来玩app破... 此事备受玩家关注!来玩app破解版,h5能反杀吗,绝活教程(有挂详细)1、打开软件启动之后找到中间准...
值得注意的是!aapoker破... 值得注意的是!aapoker破解侠是真的吗,蜀山四川游戏修改工具,经验教程(有挂助手)1、金币登录送...
第三方辅助!wepoker脚本... 第三方辅助!wepoker脚本,广东星悦有外开挂辅助器吗,法门教程(有挂分析)广东星悦有外开挂辅助器...
此事引发广泛关注!德州透视脚本... 此事引发广泛关注!德州透视脚本,崇阳斗棋辅助脚本视频,诀窍教程(的确有挂)暗藏猫腻,小编详细说明崇阳...
黑科技辅助挂!wepoker买... 黑科技辅助挂!wepoker买脚本靠谱吗,情怀七喜游戏辅助,法门教程(有挂方法)1、每一步都需要思考...
方法辅助挂!aapoker怎么... 方法辅助挂!aapoker怎么设置提高好牌几率,蘑菇云辅助使用视频,绝活儿教程(讲解有挂)1、完成蘑...