第10周——字符串:C语言世界中的大力水手(在线编程测试)

1数字字符串转换为整型数(4分)
题目内容:

从键盘输入一串字符(假设字符数少于8个),以回车表示输入结束,编程将其中的数字部分转换为整型数并以整型的形式输出。

函数原型为 int Myatoi(char str[]);

其中,形参数组str[]对应用户输入的字符串,函数返回值为转换后的整型数。

解题思路的关键是:1)判断字符串中的字符是否是数字字符;2)如何将数字字符转换为其对应的数字值;3)如何将每一个转换后的数字值加起来形成一个整型数。

程序运行结果示例1:

Input a string:7hg09y↙

709

程序运行结果示例2:

Input a string:9w2k7m0↙

9270

程序运行结果示例3:

Input a string:happy↙

0

输入提示信息:“Input a string:”

输入格式: “%7s”

输出格式:"%d\n"

注意:为避免出现格式错误,请直接拷贝粘贴上面给出的输入、输出提示信息和格式控制字符串!

时间限制:500ms内存限制:32000kb

#include<stdio.h>

int Myatoi(char str[]);

int main()
{
	char str[10];
	printf("Input a string:");
	scanf("%7s",str);
	printf("%d\n",Myatoi(str));
	return 0;
}

int Myatoi(char str[])
{
	int sum = 0;
	for(int i=0;str[i]!='\0';i++)
	{
		if(str[i] >= '0' && str[i] <= '9')
		{
			sum *= 10;
			sum += (str[i] - '0');
		}
	}
	return sum;
}

2查找子串(4分)
题目内容:

用字符数组作函数参数,编程实现在从键盘输入的字符串(假设长度小于80)中查找与指定的子串,并输出该子串在字符串中首次出现的位置,如果该字符不存在,则输出"Not found!"。

函数原型:int SearchString(char s[], char d[])

函数功能:在字符数组s中查找子串d,返回d在s中首次出现的位置,若找不到,则返回-1。

程序运行结果示例1:

Input a string:How are you!↙

Input another string:are↙

Searching results:5

程序运行结果示例2:

Input a string:hello↙

Input another string:are↙

Not found!

程序运行结果示例3:

Input a string:You are a student.↙

Input another string:you↙

Not found!

输入第一个字符串的提示信息:“Input a string:”

输入第二个字符串的提示信息:“Input another string:”

输入格式: gets()

输出格式:“Searching results:%d\n”

没找到子串的输出提示信息: “Not found!\n”

注意:为避免出现格式错误,请直接拷贝粘贴上面给出的输入、输出提示信息和格式控制字符串!

时间限制:500ms内存限制:32000kb

#include<stdio.h>
#include<string.h>

int SearchString(char s[], char d[]);

int main()
{
	char s[100];
	char d[100];
	int index;
	printf("Input a string:");
	gets(s);
	printf("Input another string:");
	gets(d);
	index = SearchString(s,d);
	if(index == -1)
	{
		printf("Not found!\n");
	}
	else
	{
		printf("Searching results:%d\n",index+1);
	}
	return 0;
}

int SearchString(char s[], char d[])
{
	int flag;
	for(int i=0;i <= strlen(s) - strlen(d);i++)
	{
		flag = 0;
		for(int j=0;j < strlen(d);j++)
		{
			if(s[i+j] == d[j])
			{
				flag++;
			}
		}
		if(flag == strlen(d))
		{
			return i;
		}
	}
	return -1;
}

3统计重复字符(4分)
题目内容:

输入一串字符(字符数小于80),以回车表示输入结束,编程计算并输出这串字符中连续重复次数最多的字符和重复次数。如果重复次数最多的字符有两个,则输出最后出现的那一个。

已知函数原型:

//函数功能:统计字符串中连续重复次数最多的字符及其重复的次数

//函数参数:str指向待统计的字符串,指针形参tag返回重复字符最后出现的下标位置

//函数返回值:返回字符重复的次数

int CountRepeatStr(char str[], int *tag);

求解思路:设置一个计数器,遍历字符串中的所有字符,若str[i] == str[i+1],则计数器加1,同时判断计数器的值是否大于记录的最大重复次数max,若大于,则用计数器的值更新max,并记录该字符最后出现的位置i+1.若str[i] != str[i+1],则计数器重新初始化为1。遍历结束时,函数返回max的值。

程序运行结果示例1:

Input a string:

2344455555↙

5:5

程序运行结果示例2:

Input a string:

sgf222257↙

2:4

输入提示信息:“Input a string:\n”

输入格式: 用gets()输入字符串

输出格式:"%c:%d\n"

注意:为避免出现格式错误,请直接拷贝粘贴上面给出的输入、输出提示信息和格式控制字符串!

时间限制:500ms内存限制:32000kb

#include<stdio.h>
#include<string.h>

int CountRepeatStr(char str[], int *tag);

int main()
{
	char str[100];
	int tag, max;
	printf("Input a string:\n");
	gets(str);
	max = CountRepeatStr(str, &tag);
	printf("%c:%d\n",str[tag],max);
	return 0;
}

int CountRepeatStr(char str[], int *tag)
{
	int max = 0;
	int count = 1;
	for(int i = 0; str[i+1] != '\0'; i++)
	{
		if(str[i] == str[i+1])
		{
			count++;
		}
		else
		{
			if(max <= count)
			{
				max = count;
				*tag = i;
			}
			count = 1;
		}
	}
	if(max <= count)
	{
		max = count;
		*tag = strlen(str) - 1;
	}
	return max;
}

4凯撒密码(4分)
题目内容:

凯撒密码是罗马扩张时期朱利斯•凯撒(Julius Caesar)创造的,用于加密通过信使传递的作战命令,其原理很简单,就是通过将字母表中的字母移动一定位置而实现加密。例如,每个字母按字母表顺序向后移3位,如a加密后变成d,b加密后变成e,……x加密后变成a,y加密后变成b,z加密后变成c。请编写一个程序,将用户从键盘输入的文本字符串(只包含a~z的字符且长度小于100)进行加密后输出。

函数原型:void Caesar(char c[]);

函数功能:计算凯撒密码

程序的运行结果示例1:

Input a string:baidu↙

edlgx

程序的运行结果示例2:

Input a string:xyz↙

abc

输入提示信息:“Input a string:”

输入格式: 用 gets()函数

输出格式:用 puts()函数

为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。

时间限制:500ms内存限制:32000kb

#include<stdio.h>
#include<string.h>

void Caesar(char c[]);

int main()
{
	char c[100];
	printf("Input a string:");
	gets(c);
	Caesar(c);
	return 0;
}

void Caesar(char c[])
{
	for(int i=0;c[i]!='\0';i++)
	{
		if(c[i] - 'x' < 3 && c[i] - 'x' >= 0)
		{
			c[i] = 'a' + (c[i] - 'x');
		}
		else
		{
			c[i] = c[i] + 3;
		}
	}
	puts(c);
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值