0

I have a string with the name "Mustang Sally Bob" After i run my code i want the string output to be like this: gnatsuM yllaS boB My approach is to count the words until the space and save the index of where the space is located in the string. then Then I want to print the characters starting from the space backwards.

#include <stdio.h>

int main()
{
    char* test="Mustang Sally Bob";

    int length; //string length
    int x;
    for(length=0;test[length] !=0&&test[length];length++); //get string length
    int counter;
    int words = 0;
    int space_index =0;
    for(counter=0;counter<length;counter++) {
        words++;
        if(test[counter]==' ') {
            space_index=counter;
            for(x=space_index-1;x>=words;x--) {
                printf("%c",test[x]);
            }
            words=0;    
            space_index = 0;
        }
    }

    return 0;
}

but when I execute this code the output I get is yllaS g does anyone know why i cant get the full string?

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
momonosuke
  • 75
  • 6
  • It looks like you are not using the `words` variable correctly. Try deleting `words++` and change `words = 0` to `words = space_index + 1;`. – 001 Dec 03 '19 at 17:39
  • Also, it won't print the last word as that is followed by `\0` and not space. – 001 Dec 03 '19 at 17:40
  • `test[length] != 0 && test[length]` is the same condition on both sides of the `&&` – yano Dec 03 '19 at 17:45
  • @JohnnyMopp thank you but when I change the code like you suggested I ` gnatsuMyllaS` I want the spaces to be included in the output too – momonosuke Dec 03 '19 at 17:50
  • @yano I don't really understand what you mean – momonosuke Dec 03 '19 at 17:50
  • @momonosuke You can just add the space after printing the reversed word with `printf(" ");` – 001 Dec 03 '19 at 17:53
  • You are relying on undefined behavior when you treat a string literal as a modifiable array. See https://stackoverflow.com/q/5464183/4996248 – John Coleman Dec 03 '19 at 17:53
  • 1
    @JohnColeman OP is not trying to modify `test` anywhere. – 001 Dec 03 '19 at 17:56
  • 1
    @JohnnyMopp You are right -- I saw the title and assumed that OP was trying to reverse the string and then print it, but I see now that they are trying to just print in a non-standard order. Nevertheless, OP should be aware that treating a string literal as an array is potentially buggy, so I'll leave my comment up. – John Coleman Dec 03 '19 at 17:58
  • Thank you all for your help. I also managed to do it with the full string and the null terminator – momonosuke Dec 03 '19 at 18:09
  • I mean you can change that `for` loop to `for(length=0; test[length] != 0; length++);` or to `for(length=0; test[length]; length++);`. Having both conditions isn't wrong, it's just redundant since both are exactly the same check. Better yet, you can simply use [`strlen`](https://linux.die.net/man/3/strlen). O/T means off-topic, I wasn't suggesting a solution to your problem at all, just a mild suggestion. – yano Dec 03 '19 at 19:09

1 Answers1

1

In general the approach is incorrect.

For example an arbitrary string can start with blanks. In this case the leading blanks will not be outputted.

The last word is ignored if after it there is no blank.

The variable words does not keep the position where a word starts.

Calculating the length of a string with this loop

for(length=0;test[length] !=0&&test[length];length++);

that can be written simpler like

for ( length = 0; test[length] != '\0' ; length++ );

is redundant. You always can rely on the fact that strings are terminated by the zero-terminating character '\0'.

I can suggest the following solution

#include <stdio.h>

int main( void )
{
    const char *test = "Mustang Sally Bob";

    for ( size_t i = 0; test[i] != '\0'; )
    {
        while ( test[i] == ' ' ) putchar( test[i++] );

        size_t j = i;

        while ( test[i] != '\0' && test[i] != ' ' ) i++;

        for ( size_t k = i; k != j; ) putchar( test[--k] );
    }

    return 0;
}

The program output is

gnatsuM yllaS boB

You could append the program with a check of the tab character '\t' if you like. In C there is the standard C function isblank that performs such a check.

Here is a demonstrative program that uses the function isblank. I also changed the original string literal.

#include <stdio.h>
#include <ctype.h>

int main( void )
{
    const char *test = " Mustang\tSally\tBob ";

    puts( test );

    for ( size_t i = 0; test[i] != '\0'; )
    {
        while ( isblank( ( unsigned char )test[i] ) ) putchar( test[i++] );

        size_t j = i;

        while ( test[i] != '\0' && !isblank( ( unsigned char)test[i] ) ) i++;

        for ( size_t k = i; k != j; ) putchar( test[--k] );
    }

    putchar( '\n' );

    return 0;
}

The program output is

 Mustang    Sally   Bob 
 gnatsuM    yllaS   boB
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335