2020年MOOCC语言程序设计精髓第十周编程题测试

1数字字符串转换为整型数
题目内容:
从键盘输入一串字符(假设字符数少于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 sum = 0,i = 0;
    while(str[i]!='\0'){
        if(str[i]<='9'&&str[i]>=0){
            sum = sum*10+str[i]-'0';
        }
        i++;
    }
    return sum;
}

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

2查找子串
题目内容:
用字符数组作函数参数,编程实现在从键盘输入的字符串(假设长度小于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 <stdlib.h>

int SearchString(char str[],char substr[]){
    int i=0,j=0,k=0;
    while(str[i]!='\0'){
        if(str[i]==substr[j]){
            while(substr[j]==str[i+k]){
                j++;
                k++;
            }
            if(substr[j]=='\0')
                return i+1;
            else
                j=k=0;
        }
        i++;
    }
    return 0;
}

int main()
{
    char str[80],substr[80];
    printf("Input a string:");
    gets(str);
    printf("Input another string:");
    gets(substr);
    if(SearchString(str,substr)!=0)
        printf("Searching results:%d\n",SearchString(str,substr));
    else
        printf("Not found!\n");
    return 0;
}

3统计重复字符
题目内容:
输入一串字符(字符数小于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 <stdlib.h>

int CountRepeatStr(char str[],int *tag){
    int i=0,times=1,max=1;
    while(str[i]!='\0'){
        if(str[i]==str[i+1]){
            times++;
            if(times>=max){
                max=times;
                *tag=i;
            }
        }
        else{
            times=1;
        }
        i++;
    }
    return max;
}

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

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 <stdlib.h>

void Caesar(char c[]){
    int i=0;
    while(c[i]!='\0'){
        c[i]=(c[i]-'a'+3)%26+'a';
        i++;
    }
}

int main()
{
    char str[100];
    printf("Input a string:");
    gets(str);
    Caesar(str);
    puts(str);
    return 0;
}
发布了33 篇原创文章 · 获赞 11 · 访问量 8475

猜你喜欢

转载自blog.csdn.net/qq_40825479/article/details/105310433
今日推荐