C Reverse words in string

Trying to learn C and so I want to reverse each word in string. "Hello World" to "olleH dlroW" This is what I have so far.

int main()
{
    char str[100];
    int i;
    printf("Enter string:");
    fgets(str,sizeof(str),stdin);

    for (i = 0; i <= strlen(str); i++)
    {
        if (str[i] == ' ')
        {
            // Here the space and how should I switch words now?
        }
    }

    return 0;
}

Should I do something like this:

temp = str[i]; j = str[i-1];

and then switch places? str[i]=j; and j=temp; I am stuck at this point here!

Answers


I won't give away the answer because this seems like a homework problem and is a great opportunity to learn, but here is a hint:

Think about how you could programatically reference the 'opposite' of each letter in the word. So str[0] and str[strlen(str)], and so on. That should help you, but feel free to probe with further questions.


Try this...

private void Button1_Click(object sender, EventArgs e)
{
    const string targetWords = "Hello World";
    int count = targetWords.Length;
    string result = null;
    string chr = null;

    while (!(count == 0)) {
        chr = targetWords.Substring((count - 1), 1);
        result += chr;
        count -= 1;
    }
    Debug.WriteLine(result);

}

Need Your Help

Why doesnt the AsyncCallback update my gridview?

gridview asynchronous begininvoke

I started working with delegates last week and i am trying to update my gridview async on the background. All goes well, no errors or such but i dont get a result after my EndInvoke. does anyone know

What is the lucene query syntax for not in list?

syntax lucene

I've done some google searching and haven't found exactly what I'm looking for. Basically I have a list of item in a lucene index, and I start with getting a set of items. I want to follow up that