I am new to programming.I have written this code which arranges the words in reverse order i.e.,
if input is "I Am Sad"
output should be "Sad Am I"
but am not able to get where i am going wrong
so plz help me.
here is the code

#include<stdio.h>
#include<string.h>
main()
{
 char s[]="I Am Sad";
 char str[10];int i;
 char *st2;
 char *st;
 st=&s[0];
while(*st!='\0')
{
    if(*st==' ')
    st2=st;
    *st++;
}
 for(;strlen(str)!=strlen(s);)
  {
    *st2++;
    st=st2;
    for(i=0;(*st!='\0')&&(*st!=' ');*st++,i++)
    str[i]=*st;
    str[i]=' ';
    for(;*st2!=' ';*st2--);
}
    str[i]='\0';
    puts(str);
}

Recommended Answers

All 3 Replies

Please review the following code and compare with yours. Take note of the comments and try to understand why I made the changes that I made. I tried to keep the spirit of the algorithm the same, even though I'd probably do this differently:

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

int main(void)
{
    char s[] = "I Am Sad";
    char str[10] = ""; /* Make sure to initialize str because its length is taken shortly */
    int i = 0;
    char *st2;
    char *st;

    st = &s[0];

    while (*st != '\0') {
        if (*st == ' ')
            st2 = st;

        *st++;
    }

    for (; strlen(str) < strlen(s);) {
        /* What if st2 == s? */
        if (*st2 == ' ')
            ++st2;

        st = st2;

        /* Don't reset i if you want to append to str */
        while (*st != '\0' && *st != ' ')
            str[i++] = *st++;

        str[i++] = ' ';

        /* Don't forget to skip whitespace or you'll always process the same word */
        while (st2 != s && *--st2 == ' ')
            ;

        /* Now find the previous word. Don't forget to check for the start of the string */
        while (st2 != s && *st2 != ' ')
            --st2;
    }

    str[i] = '\0';
    puts(str);

    return 0;
}

Thank you deceptikon.It helped a lot.Also i would like to know the other methods of doing this.

Also i would like to know the other methods of doing this.

My solution would probably be a double reversal. First reverse the entire string character by character (ie. "daS mA I"). Then go over the string again and only reverse each word. The end result is that the word order is reversed, but the characters in each word are normal (ie. "Sad Am I").

I'd also tighten up the definition of a word to just alphabetic and numeric characters. That way any punctuation can be ignored in the second step and retains the original position. But that's straying a bit from the problem definition and may need tweaking if it's not the desired result:

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

void reverse_string(char *begin, char *end)
{
    while (begin < end) {
        char temp = *begin;
        *begin++ = *--end;
        *end = temp;
    }
}

char *word_begin(char *s) { while (*s && !isalnum(*s)) { ++s; } return s; }
char *word_end(char *s) { while (*s && isalnum(*s)) { ++s; } return s; }

int main(void)
{
    char s[] = "The quick brown fox jumps over the lazy dog";
    char *begin = s, *end;

    reverse_string(s, s + strlen(s)); /* Reverse everything */

    /* Reverse individual words to restore them */
    while (*begin) {
        begin = word_begin(begin);
        end = word_end(begin);
        reverse_string(begin, end);
        begin = end;
    }

    puts(s);

    return 0;
}
commented: good approach uses! +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.