C语言实验 字符串

实验九 字符串

实验目的
1.掌握字符串的访问和输入输出方法
2.掌握字符串处理函数的应用
3.掌握向函数传递字符串的方法
4.掌握从函数返回字符指针的方法
实验条件:
PC 计算机,Windows7 或 Windows10 操作系统,Office2010 及以上版本,
Dev-C++5.11 及以上版本或 Code Blocks16.01 及以上版本。
实验内容
1.程序调试
找最小的字符串:输入 5 个字符串,输出其中最小的字符串。
源程序(有错误)

#include<stdio.h> 
#include<string.h> 
int main() 
{ 
	int i; 
	char str[80], 
	min[80]; 
	printf("Input 5 strings:\n"); 
	scanf("%s", str); 
	min=str; 
	for(i=1; i<5; i++){ 
	//调试时设置断点 
		scanf("%s", str); 
		if(min>str){ 
			min=str; 
		} 
	}
	printf("Min is: %s", min); 
	return 0;} 

程序清单如下:

#include<stdio.h> 
#include<string.h> 
int main() 
{ 
	int i; 
	char str[80], 
	min[80]; 
	printf("Input 5 strings:\n"); 
	scanf("%s", str); 
	strcpy(min,str); 
	for(i=1; i<5; i++){ 
	//调试时设置断点 
		scanf("%s",str); 
		if(strcmp(min, str)>0){ 
			strcpy(min,str); 
			} 
	}
	printf("Min is: %s", min); 
	return 0; 
}

该程序的运行结果如图一所示: 在这里插入图片描述

图一
2.基础编程
(1)假设字符串内字符数不超过 80 个,利用指针编程,统计给定字符串中
大写字母、小写字母和其他字符分别有多少个。要求用函数实现功能,主程序只
完成数据的输入输出。
函数原型:

void StatChar(char *str, int *capital, int *lowcase, int *other); 
void StatChar(char str[], int *capital, int *lowcase, int *other); 

输入提示信息:"Input string:\n"输入格式:
用 gets()函数
输出格式:” capital:%d, lowcase:%d, other:%d\n”
运行样例
Input
string:
I can
play
the piano.↙
capital:1,
lowcase:15, other:5
程序清单如下:

#include<stdio.h> 
void StatChar(char *str, int *capital, int *lowcase, 
int 
*other); 
int main() 
{ 
	char str[80]; 
	int capital=0,lowcase=0,other=0; 
	printf("Input string:\n"); 
	gets(str); 
	StatChar(str,&capital,&lowcase,&other); 
	printf(" capital:%d, lowcase:%d,other:%d\n",capital,lowcase,other); 
	return 0; 
}
void StatChar(char *str, int *capital, int *lowcase, int *other) 
{ 
	while(*str!='\0') 
	{ 
		if((*str>='A')&&(*str<='Z')) 
		{ 
			(*capital)++; 
		}
		else if((*str>='a')&&(*str<='z')) 
		{ 
			(*lowcase)++; 
		}
		else 
		{ 
			(*other)++; 
		}
		str++; 
	} 
}

该程序的运行结果如图二所示:
在这里插入图片描述
图二
(2)假设字符串内字符数不超过 80 个,分别用字符数组和字符指针做函数
参数两种方法实现下述功能:在字符串中删除与某字符相同的字符。
函数原型:

void DelChar(char *str, char ch) 
void DelChar(char str[ ] , char ch) 

输入提示信息:“Input string:\n”
“Character to be deleted:\n”
输入格式:
用 gets()函数
输出格式:”%s”
运行样例
Input string:
you are my sunshine.↙
Character
to be deleted:
e↙
you ar my
sunshin.
程序清单如下(使用指针):

#include<stdio.h> 
void DelChar(char *str, 
char ch); 
int main() 
{ 
	char str[80],ch; 
	printf("Input string:\n"); 
	gets(str); 
	printf("Character to be deleted:\n"); 
	scanf("%c",&ch); 
	DelChar(str,ch); 
	printf("%s",str); 
	return 0; 
}
void DelChar(char *str, char ch) 
{ 
	char *p; 
	while(*str!='\0'){ 
		if(*str==ch) 
		{ 
			p=str; 
			while(*str!='\0') 
			{ 
				*str=*(str+1); 
				str++; 
			}
		str=p; 
		}
	str++; 
	} 
}

该程序的运行结果如图三所示:在这里插入图片描述

图三
程序清单如下(使用数组):

#include<stdio.h> 
void DelChar(char str[ ] , 
char ch); 
int main() 
{ 
	char str[80],ch; 
	printf("Input string:\n"); 
	gets(str); 
	printf("Character to be deleted:\n"); 
	scanf("%c",&ch); 
	DelChar(str,ch); 
	printf("%s",str); 
	return 0; 
}
void DelChar(char str[ ] , char ch) 
{
	int k=0; 
	int i=0; 
	while(str[i]!='\0') 
	{ 
		if(str[i]==ch) 
		{ 
			k=i; 
			do
			{ 
				str[i]=str[i+1]; 
				i++; 
			}while(str[i]!='\0'); 
			i=k; 
			}
		i++; 
	} 
}

该程序的运行结果如图四所示: 在这里插入图片描述

图四

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xlorb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值