九度题目1525:子串逆序打印

来源:互联网 发布:lol盲僧怎么玩 知乎 编辑:程序博客网 时间:2024/06/05 18:16

题目描述:

小明手中有很多字符串卡片,每个字符串中都包含有多个连续的空格,而且这些卡片在印刷的过程中将字符串的每个子串都打印反了,现在麻烦你帮小明将这些字符串中的子串修正过来,同时为了使卡片美观,压缩其中的连续空格为1个。

输入:

输入包含多个测试用例,每个测试用例的第一行是一个正整数 n,1=<n<=100000,代表卡片上字符串的长度。第二行输入长度为n的字符串(字符串仅包含小写字母和空格)。当n为0时,代表输入结束。

输出:

对应每个测试用例,请按照要求输出修正过的字符串。

样例输入:

3

abc

13

abc   efg hij

样例输出:

cba

cba gfe jih


自己写的测试小程序:

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

char *Delete_Blank_Char(char *input, int n)
{
    char *tmp = NULL;
    int size = 100000;
    char *result = (char *)malloc(size);
    int i = 0, j = 0, k = 0, flag = 0;

    tmp = input;
    while(i < n)
    {
        if(tmp[i] == ' ')
        {
            if(k == i)  //针对第一个单词前面就有空格的情况做处理
            {
                k++;
                i++;
            }
            else  //只保留一个空格的处理
            {
                if(flag)
                {
                    result[j++] = ' ';
                    i++;
                }
                else
                {
                     i++;
                }
            }
            flag = 0;
        }
        else
        {
            result[j++] = tmp[i++];
            flag = 1;
        }
    }
    result[j] = '\0';

    return result;
}

char *Rev_Chars(char *str)
{
    char *start = str;
    char *left = str;
    char ch;

    while(*str++)
        ;
    str -=2;

    if(str != NULL)
    {
        while(left < str)
        {
            ch = *left;
            *left++ = *str; //这里是先赋值再++,即先*start = *end,再执行*start++
            *str-- = ch;
        }
    }

    return start;
}

char *Rev_Every_Words(char *str)
{
    int i = 0, flag = 0, n = 0, len = 0;
    int size = 1024;
    char *tmp = malloc(size);
    char *result = malloc(size);

    len = strlen(str);
    for(i = 0; i < len; i++)
    {
        if(str[i] != ' ')
        {
            tmp[n] = str[i];
            n++;
            flag = 1;
        }
        else
        {
            if(flag)
            {
                result = Rev_Chars(tmp);
                printf("%s",result);
                printf(" ");
                flag = 0;
                n = 0;
                memset(tmp, 0, size);
            }
        }
    }
    result = Rev_Chars(tmp);
    printf("%s", tmp);

    return result;
}


int main(void)
{
    int n = 0, i = 0, size = 100000, len = 0;
    char *input = (char *)malloc(size);
    char *revstr = (char *)malloc(size);
    char *result = (char *)malloc(size);

    while(1)
    {
        printf("\n");
        printf("Please input the length of chars(1-10000):\n");
        printf("if you enter the number of 0,the program will exit.\n");
        scanf("%d",&n);
        printf("-----------------------------------\n");
        if(0 == n)
        {
            break;
        }
        while(getchar() != '\n')
        {
            continue;
        }
        fflush(stdin);
        fgets(input, size, stdin);

        if(strlen(input) > n+1)
        {
            printf("the length of input chars is too long !\n");
            printf("-----------------------------------\n");
            continue;
        }
        result = Delete_Blank_Char(input, n);
        printf("-----------------------------------\n");

        Rev_Every_Words(result);
    }

    return 0;
}


0 0
原创粉丝点击