第10周编程题在线测试

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"

#include<stdio.h>
int Myatoi(char str[])
{
	int i,sum=0,p=1,r=0,a[100]={0};
    for(i=0;i<8;i++)
	{
		if(str[i]>=48&&str[i]<=57)
		{
			a[r]=(int)str[i]-48;
			r++;
		}
	}
	for(i=r-1;i>=0;i--)
	{
		sum+=a[i]*p;
		p=p*10;
	}
return sum;
}
main()
{
	char str[100];
	printf("Input a string:");
	scanf("%7s",str);
	printf("%d\n",Myatoi(str));
	return 0;
}

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”

#include<stdio.h>
#include<string.h>
int SearchString(char s[],char d[])
{
	int a,b,i,l1,l2;
	l1=strlen(s);
	l2=strlen(d);
	for(i=0;i<l1;i++)
	{
		if(s[i]==d[0])
		{
		    a=1;
		  for(b=0;b<l2;b++)
		  {
            if(s[i+b]!=d[b])
				a=0;
		  }
		  if(a)
		  return (i+1);
		}
	}
	return 0;
}
main()
{
   char s[100],d[100];
   printf("Input a string:");
    gets(s);
   printf("Input another string:");
    gets(d);
	if(SearchString(s,d)!=0)
		printf("Searching results:%d\n",SearchString(s,d));
    else
		printf("Not found!\n");
	return 0;
}

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"

#include<stdio.h>
#include<string.h>
int CountRepeatStr(char str[], int *tag)
{
	int l=strlen(str);
	int i,max=1,count=1;
    for(i=0;i<l;i++)
	{
		if(str[i]==str[i+1])
		{
			count++;
			if(count>max)
			{
				max=count;
				*tag=i+1;
			}
		}
		else
		  count=1;
	}
	return max;
}
main()
{
	char str[80]={0};
	int tag=0;
	printf("Input a string:\n");
	gets(str);
    printf("%c:%d\n",str[tag],CountRepeatStr(str,&tag));
	return 0;
}

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()函数

#include<stdio.h>
#include<string.h>
void Caesar(char c[])
{
   int i,l=strlen(c);
   for(i=0;i<l;i++)
   {
	   c[i]=c[i]+3;
	   if(c[i]>122)
		   c[i]=c[i]-26;
   }
   puts(c);
}
main()
{
	char c[100]={0};
	printf("Input a string:");
	 gets(c);
    Caesar(c);
	return 0;
}
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值