1
votes

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.

// 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
SOrt out the indetation. BTW - Are these lecturers getting better at the problems they are setting students. @userX - Learn to use the debuggerEd Heal
Your logic is wrong. Frame the logic using pen and paper, test it manually, then write the program.Don't You Worry Child
@user3551403 - WhozCraig (thanks) fixed it for youEd Heal
I dont get the description... If an input is 'bar dog' what output do you want: 'dog bar' or 'rab god' or may be 'god rab'...?CiaPan
@CiaPan I just want to reverse d alphabets of words but retain the order of words. output of 'bar dog' should be 'rab god'Bob

5 Answers

2
votes

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.

#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;   
}
0
votes

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

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

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

//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");
}
0
votes
#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--]);
    }
}