简体   繁体   English

c程序,用于向后打印字符串中的每个单词

[英]c program for printing each word in string backwards

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++;
    }
}

Do not use gets . 不要使用gets It's not safe and is deprecated. 它不安全,已弃用。 Use fgets instead to read an input string stdin . 改用fgets读取输入字符串stdin Now, to print each word in the string backwards, you need to change your while loop. 现在,要向后打印字符串中的每个单词,您需要更改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;   
}

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");
}

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 这样的内循环

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++;
}

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--]);
    }
}
#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--]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM