c程序用于将字符串中的每个字向后打印。

时间:2022-01-28 20:00:06

this is my source code for printing each word in string backwards. but this code is just printing the 1st word backward and not the entire string. after printing 1st word backward it generates a pattern of 1st and 2nd words printed backwards. If while is used instead of if then it generates an infinite loop.

这是我的源代码,用于在字符串中向后打印每个单词。但是这段代码只是将第一个单词打印出来,而不是整个字符串。在打印第一个单词后,它会生成一个第一个和第二个单词的图案。如果使用while而不是If,那么它将生成一个无限循环。

// Print an Entered String Backwards
#include <stdio.h>
#include <string.h>

int main()
{
    int j,i;
    char str[100];
    printf("Enter String\n");
    gets(str);
    printf("\nString in Reverse Order\n");
    i=0;
    while(str[i]!='\0')
    {
        if(str[i]==' ')
        {
            for(j=i-1;j>=0;j--)         //Loop starts from last char and decrements upto 0th char
                printf("%c",str[j]);
            printf(" ");
        }
        i++;
    }
}

5 个解决方案

#1


2  

Do not use gets. It's not safe and is deprecated. Use fgets instead to read an input string stdin. Now, to print each word in the string backwards, you need to change your while loop.

不要使用。它不安全,不赞成。使用fgets来读取输入字符串stdin。现在,要在字符串中向后打印每个单词,您需要更改while循环。

#include <stdio.h>

int main(void)
{
    int i, j;
    char str[100];
    printf("Enter String\n");

    // fgets reads one less than sizeof(str), i.e., 99
    // characters from stdin and stores them in str array.
    // it returns when a newline is input which it stores in
    // the the array and then appends a terminating null byte
    // to mark the end of the string
    fgets(str, sizeof str, stdin);
    printf("\nString in Reverse Order\n");

    i = 0;
    while(str[i] != '\0')
    {
        if(str[i] == ' ' || str[i] == '\n')
        {   
            // the loop condition checks for either the 
            // start of the string or a whitespace since
            // since these two cases demarcate a word from
            // other words in the string
            for(j = i - 1; j >= 0 && str[j] != ' '; j--)
                printf("%c", str[j]);

            printf(" ");    
        }
        i++;
    }
    // output a newline to flush the output
    printf("\n"); 
    return 0;   
}

#2


0  

Every time you are testing the inner loop to zero, which will print till the start of the string each time. You need to just test till last space found. Something like this for inner loop

每次测试内循环到0的时候,每次都要打印到字符串的开头。你只需要测试到最后的空间。像这样的内部循环。

int lastSpace = 0, i = 0, j = 0;
while(str[i] != '\0')
{
if(str[i] == ' ')
{
for(j = i-i; j>= lastSpace; j++)
{
   printf("%c", str[j]);
}
lastSpace = i;
}
i++;
}

#3


0  

No need to double loop you can do in single loop.

不需要在单循环中进行双循环。

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

int main()
{
    int j,i, len;
    char str[100];
    clrscr();
    printf("Enter String\n");
    gets(str);
    printf("\nString in Reverse Order\n");
    i = strlen(str) - 1;
    while(i > -1)
    {
    printf("%c",str[i--]);
    }
}

#4


0  

The loop you commented shouldn't end on the 0th char, but should go upto the last white space( if there was one ).

您注释的循环不应该以第0个字符结束,而是应该向上到最后一个空白区域(如果有的话)。

//Print an Entered String Backwards
#include<stdio.h>
#include<string.h>

int main()
{
    int j,i; int lastWhiteSpace = 0; //there were no white spaces
    char str[100];

    printf("Enter String\n");

    gets(str);

    printf("\nString in Reverse Order\n");

    i=0;
    while(str[i]!='\0')
    {
        if(str[i]==' ')
        {
            for(j=i-1;j>=lastWhiteSpace;j--)  //Loop starts from last char and decrements upto the character after the last white space 
                printf("%c",str[j]);
            printf(" ");
            lastWhiteSpace = i + 1; //the character after this white space 
        }
        i++;
    }

    //later edit
    for(j=i-1;j>=lastWhiteSpace;j--)
        printf("%c",str[j]);
    printf("\n");
}

#5


0  

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

int main()
{
    int j,i, len;
    char str[100];
    char temp[20];
    clrscr();
    printf("Enter String\n");
    gets(str);
    printf("\nString in Reverse Order\n");
    i = 0;
    len = 0;
    j = 0;
    while(str[i] != '\0')
    {
        if(str[i] != ' ')
            temp[j++] = str[i];

        if(str[i] == ' ')
        {
            temp[j] = '\0';
            len = strlen(temp);

            while(len > -1)
            {
                printf("%c", temp[len--]);
            }
            printf(" ");
            len = 0;
            j = 0;
        }
    i++;
    }
    temp[j] = '\0';
    len = strlen(temp);
    printf(" ");
    while(len > -1)
    {
        printf("%c",temp[len--]);
    }
}

#1


2  

Do not use gets. It's not safe and is deprecated. Use fgets instead to read an input string stdin. Now, to print each word in the string backwards, you need to change your while loop.

不要使用。它不安全,不赞成。使用fgets来读取输入字符串stdin。现在,要在字符串中向后打印每个单词,您需要更改while循环。

#include <stdio.h>

int main(void)
{
    int i, j;
    char str[100];
    printf("Enter String\n");

    // fgets reads one less than sizeof(str), i.e., 99
    // characters from stdin and stores them in str array.
    // it returns when a newline is input which it stores in
    // the the array and then appends a terminating null byte
    // to mark the end of the string
    fgets(str, sizeof str, stdin);
    printf("\nString in Reverse Order\n");

    i = 0;
    while(str[i] != '\0')
    {
        if(str[i] == ' ' || str[i] == '\n')
        {   
            // the loop condition checks for either the 
            // start of the string or a whitespace since
            // since these two cases demarcate a word from
            // other words in the string
            for(j = i - 1; j >= 0 && str[j] != ' '; j--)
                printf("%c", str[j]);

            printf(" ");    
        }
        i++;
    }
    // output a newline to flush the output
    printf("\n"); 
    return 0;   
}

#2


0  

Every time you are testing the inner loop to zero, which will print till the start of the string each time. You need to just test till last space found. Something like this for inner loop

每次测试内循环到0的时候,每次都要打印到字符串的开头。你只需要测试到最后的空间。像这样的内部循环。

int lastSpace = 0, i = 0, j = 0;
while(str[i] != '\0')
{
if(str[i] == ' ')
{
for(j = i-i; j>= lastSpace; j++)
{
   printf("%c", str[j]);
}
lastSpace = i;
}
i++;
}

#3


0  

No need to double loop you can do in single loop.

不需要在单循环中进行双循环。

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

int main()
{
    int j,i, len;
    char str[100];
    clrscr();
    printf("Enter String\n");
    gets(str);
    printf("\nString in Reverse Order\n");
    i = strlen(str) - 1;
    while(i > -1)
    {
    printf("%c",str[i--]);
    }
}

#4


0  

The loop you commented shouldn't end on the 0th char, but should go upto the last white space( if there was one ).

您注释的循环不应该以第0个字符结束,而是应该向上到最后一个空白区域(如果有的话)。

//Print an Entered String Backwards
#include<stdio.h>
#include<string.h>

int main()
{
    int j,i; int lastWhiteSpace = 0; //there were no white spaces
    char str[100];

    printf("Enter String\n");

    gets(str);

    printf("\nString in Reverse Order\n");

    i=0;
    while(str[i]!='\0')
    {
        if(str[i]==' ')
        {
            for(j=i-1;j>=lastWhiteSpace;j--)  //Loop starts from last char and decrements upto the character after the last white space 
                printf("%c",str[j]);
            printf(" ");
            lastWhiteSpace = i + 1; //the character after this white space 
        }
        i++;
    }

    //later edit
    for(j=i-1;j>=lastWhiteSpace;j--)
        printf("%c",str[j]);
    printf("\n");
}

#5


0  

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

int main()
{
    int j,i, len;
    char str[100];
    char temp[20];
    clrscr();
    printf("Enter String\n");
    gets(str);
    printf("\nString in Reverse Order\n");
    i = 0;
    len = 0;
    j = 0;
    while(str[i] != '\0')
    {
        if(str[i] != ' ')
            temp[j++] = str[i];

        if(str[i] == ' ')
        {
            temp[j] = '\0';
            len = strlen(temp);

            while(len > -1)
            {
                printf("%c", temp[len--]);
            }
            printf(" ");
            len = 0;
            j = 0;
        }
    i++;
    }
    temp[j] = '\0';
    len = strlen(temp);
    printf(" ");
    while(len > -1)
    {
        printf("%c",temp[len--]);
    }
}