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

相关内容

热门资讯

黑科技辅助!wpk辅助神器(透... 黑科技辅助!wpk辅助神器(透视)软件透明辅助挂(本来是真的有挂)-哔哩哔哩是一款可以让一直输的玩家...
5分钟了解“创思维正版辅助器下... 5分钟了解“创思维正版辅助器下载”详细透视开挂辅助安装-哔哩哔哩;一、创思维正版辅助器下载有挂的是的...
两分钟科普!wpk真吗,哈糖大... 两分钟科普!wpk真吗,哈糖大菠萝可以开挂吗,曝光教程(发现有挂)-哔哩哔哩哈糖大菠萝可以开挂吗辅助...
第一分钟了解(昆仑大厅)外挂辅... 第一分钟了解(昆仑大厅)外挂辅助插件(透视)详细教程(2022已更新)(哔哩哔哩);亲真的是有正版授...
黑科技辅助!wpk俱乐部长期盈... 黑科技辅助!wpk俱乐部长期盈利打法(透视)软件透明挂黑科技(切实存在有挂)-哔哩哔哩;1、让任何用...
第6分钟了解“功夫川嘛辅助器”... 第6分钟了解“功夫川嘛辅助器”详细透视开挂辅助器-哔哩哔哩;人气非常高,ai更新快且高清可以动的一个...
第五分钟辅助!xpoker辅助... 第五分钟辅助!xpoker辅助,德州透视插件,攻略教程(有挂方法)-哔哩哔哩德州透视插件辅助器中分为...
两分钟了解(皮皮跑胡子)外挂透... 两分钟了解(皮皮跑胡子)外挂透明挂辅助工具(辅助挂)透明挂教程(2020已更新)(哔哩哔哩);皮皮跑...
黑科技辅助!微扑克可以加入俱乐... 您好,微扑克可以加入俱乐部这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
8分钟了解“掌中乐游戏中心辅助... 8分钟了解“掌中乐游戏中心辅助器”详细透视开挂辅助脚本-哔哩哔哩;1、这是跨平台的掌中乐游戏中心辅助...