0

I tried to use C array to write about the code below.
When I entered=>I am a boy.
The desired output is=>boy a am I.
Here is my code below and it does not work.
I have found the bugs for 2 days and I still could not find the problem

int i,j,k,m,start,temp_end,len;
char str[30];
gets(str);

len=strlen(str);
temp_end=len-1;

for(k=len;k>0;k--)
{
    if(str[k]==" ")
        start=k;
    for(j=start+1;j<temp_end;j++)
    {
        printf("%c",str[j]);
    }
    printf(" ");
    temp_end=k;
}
for(m=0;m<temp_end;m++)
{
    printf("%2c.",str[m]);
}
Himanshu
  • 4,327
  • 16
  • 31
  • 39
Hardy
  • 9
  • 1
  • In `if(str[k]==" ")`, the problem is that `" "` is a string, not a character. Try `if(str[k]==' ')` instead. – user3386109 Aug 02 '16 at 03:58
  • 1
    "it does not work". So what does it do? Have you tried using a debugger to help you find the problem? But for starters `if(str[k]==" ")` is wrong as `str[k]` is a `char` but `" "` is a string. – kaylum Aug 02 '16 at 04:00
  • sorry I have to change my words – Hardy Aug 02 '16 at 04:03
  • does not the desired ouyput – Hardy Aug 02 '16 at 04:05
  • Replace `if(str[k]==" ")` with `if(str[k] == ' ')` (the single quotes do matter). – Doc Aug 02 '16 at 04:09
  • 1
    Your code has multiple problems so it is hard to help you without essentially rewriting your code. So I ask again - have you used a debugger? If you trace your program that way you will clearly see that the first `printf` `for` loop needs to be inside the preceding `if` block. That is, should only print the word *after* a space is found and not for every iteration of the outer loop. There are other problems. – kaylum Aug 02 '16 at 04:12
  • yes i use the debugger. – Hardy Aug 02 '16 at 04:15
  • Welcome to Stack Overflow. Please read the [About] page and [Ask] page soon — but read about how to create an MCVE ([MCVE]) now. Can you describe the algorithm you're trying to implement? How do you print the words in reverse order? How do you split the string into words? How do you print those words in reverse order? It is not remotely clear to me what you're trying to do. It is clear that it doesn't do what you need, though. – Jonathan Leffler Aug 02 '16 at 04:15
  • 1
    Please also read [Why the `gets()` function is too dangerous to be used](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) — ever, even in test code. – Jonathan Leffler Aug 02 '16 at 04:15
  • By reading your code I saw some simple problems: len=strlen(str); and for(k=len;k>0;k--) and if(str[k]==" ") you are never passing in str[0] and you are checking '\0' in the first loop – Arnaud Aliès Aug 02 '16 at 07:19

1 Answers1

1

As others have pointed out there are multiple problems with your code.

  1. In the condition if(str[k]==" "), you are using " " to represent a single space character, which is incorrect. For a single space character you need to use ' '. A " " is a string with a single space character.

  2. You are running the for(j=start+1;j<temp_end;j++) { … } loop every time. However this should be run only when a new space, ' ' is found. You can use a flag for this. Same with printf(" ");, and temp_end = k;

  3. printf("%2c.", str[m]) looks to be like a typo. Why %2c.? You should be doing just "%c".


    /* Use a flag to know when to print */
    /* 0 means don't print, 1 means print */
    int pflag = 0;

    for(k = len-1; k >0; k--)
    {
            if(str[k]==' ') {
                    pflag = 1; /* new space found, update the flag to print  */
                    start=k;
            }
            if (pflag == 1)   /* check flag to see if print needed */
            { 
                    for(j=start+1;j<temp_end;j++)
                    {
                            printf("%c",str[j]);
                    }
                    printf(" ");
                    temp_end=k;
                    pflag = 0;   /* reset flag after printing */
            }
    }

    for(m=0;m<temp_end;m++)
    {
            printf("%c",str[m]);
    }

Also, do NOT use gets(). Use fgets() or something else.

Community
  • 1
  • 1
sps
  • 2,720
  • 2
  • 19
  • 38