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

相关内容

热门资讯

每日必看教程!微乐小程序黑科技... 每日必看教程!微乐小程序黑科技免费(辅助器),微信小程序怎么开挂(有辅助挂教学)1、让任何用户在无需...
今日公布!微乐小程序免费黑科技... 今日公布!微乐小程序免费黑科技(透视器),微信小程序微乐辅助器教程图片(有辅助挂辅教程)1、完成微乐...
安装程序教程!微乐小程序辅助工... 安装程序教程!微乐小程序辅助工具(透视器),微乐小程序黑科技辅助工具2026最新推荐(证实有辅助挂)...
我来教大家!微乐小程序黑科技免... 我来教大家!微乐小程序黑科技免费(辅助),微信小程序微乐安徽辅助苹果(确实有辅助挂)1)微乐小程序黑...
热点推荐!微乐小程序黑科技免费... 热点推荐!微乐小程序黑科技免费(辅助),微信小程序透视微乐(竟然有辅助挂)1、微乐小程序黑科技免费破...
揭秘几款!微乐小程序免费黑科技... 揭秘几款!微乐小程序免费黑科技(辅助),微信微乐陕西小程序破解器(有辅助挂辅助)揭秘几款!微乐小程序...
重大通报!微乐小程序免费黑科技... 重大通报!微乐小程序免费黑科技(辅助),微乐游戏辅助脚本安装(有辅助挂辅助)1、每一步都需要思考,不...
玩家交流!微乐小程序免费黑科技... 玩家交流!微乐小程序免费黑科技(作弊器),微乐家乡官方app下载(有辅助挂辅教程)1)微乐小程序免费...
教程攻略!微乐小程序黑科技免费... 教程攻略!微乐小程序黑科技免费(开挂),微乐小程序游戏破解器下载(有辅助挂方式)1、不需要AI权限,...
终于清楚!微乐小程序黑科技免费... 终于清楚!微乐小程序黑科技免费(作弊器),微乐小程序辅助教程(有辅助挂秘籍)1、用户打开应用后不用登...