美文网首页C语言学习C语言杭电oj
第十周--编程题在线测试

第十周--编程题在线测试

作者: 小小Henry | 来源:发表于2019-12-17 21:21 被阅读0次

    1数字字符串转换为整型数

    #include<stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    #define N 8
    
    int Transform(char str[]);
    
    int main(void)
    {
        char str1[N+1];
        printf("Input a string:");
        gets(str1);
        printf("%d\n", Transform(str1));
        return 0;
    }
    
    int  Transform(char str[])
    {
        char a[8];
        int counter=0;
        for (int i = 0; *(str + i) != '\0'; i++)
        {
            if (*(str + i) <= 57 && *(str + i) >= 48)
            {
                *(a + counter) = *(str + i);
                counter++;
            }
        }
        *(a + counter) = '\0';
        return atoi(a);
    
    }
    

    2查找子串

    #include<stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    int SearchString(char str[], char astr[]);
    
    int main(void)
    {
        char str[80];
        char astr[80];
        int Position;
        printf("Input a string:");
        gets(str);
        printf("Input another string:");
        gets(astr);
        Position = SearchString(str, astr);
        if (Position == -1)
            printf("Not found!\n");
        else
            printf("Searching results:%d\n", Position);
        return 0;
    }
    
    int SearchString(char str[], char astr[])
    {
        char a[80];
        int i = 0;
        int m,n;
        int counter = 0;
        int lenas = strlen(astr);
        for (i = 0; *(str + i) != '\0'; i++)
        {
            if (*(str + i) == *astr)
            {
                for (m = 0, n = i; m < lenas; m++, n++)
                    if (*(str + n) == *(astr + m))
                        counter++;
                if (counter == lenas)
                    return i + 1;
                else
                    counter = 0;
            }
        }
        return -1;
    }
    

    3统计重复字符

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

    4凯撒密码

    #include<stdio.h>
    #include <string.h>
    #include<stdlib.h>
    
    void Caesar(char str[]);
    
    int main(void)
    {
        char str[100];
        printf("Input a string:");
        gets(str);
        Caesar(str);
        return 0;
    }
    
    
    void Caesar(char str[])
    {
        int i;
        for (i = 0; *(str + i) != 0; i++)
        {
            if (*(str + i) < 'x')
                *(str + i) += 3;
            else
                *(str + i) -= 23;
        }
        *(str + i) = 0;
        puts(str);
    }
    

    相关文章

      网友评论

        本文标题:第十周--编程题在线测试

        本文链接:https://www.haomeiwen.com/subject/sjbenctx.html