can an1 plz :)help me to write a program to reverse the individual words present in a string ?? exampe:- my name is raju
ym eman si ujar

Recommended Answers

All 9 Replies

[boilerplate_help_info]

Posing requests for help must be well thought out if you want help quickly and correctly.  Your post did not meet the criteria for quality help. You may get some posts, but are they going to be useful?  Check your post with these checkpoints - what is it [i]you[/i] missed:
[list=1]
[*]Ask a question that can be answered. Do not ask
- What's wrong with my code?
- Why doesn't this work?
- Anything else that does not give us useful information
[*]Post your code.  If we don't know what you did, how can we possibly help?
- Use [b]PROPER FORMATTING[/b] -- see this
- Use CODE Tags so your formatting is preserved.
If we can't follow your code, it's difficult to help. We don't care that you're still working on it. If you want us to read it, it must be readable
[*]Explain what the code is supposed to do.  If we don't know where the target is, how can we help you hit it?
[*]Explain what actually happened! If we don't know where the arrow went when you shot it, how can we tell what went wrong and how far from the target you are?
[*]If you have errors, post them! We can't see your screen.  We can't read your mind. You need to tell us what happened.
[*]Do [b]not[/b] ask for code. We are not a coding service. We will help you fix your code. 
    If anyone posts working code for you, they are a cheater. 
    If you use that code [i]you[/i] are a cheater.
[*]Do [b]not[/b] bore us with how new you are. We can tell by your code.
- Do not apologize. We were all new, and unless you are completely 
  brain dead you will get better.
- Do not ask us to "take it easy on you."
- Do not say "I don't know what's going on." That's obvious since
  you posted for help. Use that time wisely by [b]explaining[/b] as best 
  you can so we can help.
[*][b]Do not post your requirements and nothing else. [/b]We view that as a lazy do-nothing student that wants us to do their work for them. That's cheating and we [i]will[/i] be hard on you.
[*]Do not attach files except when absolutely necessary. Most of us are not going to download file.  Add the information to your post.
[*][b]Do not tell us how urgent it is.[/b] Seriously, for us there is no urgency at all. Many that can help will ignore any URGENT or ASAP requests.
[/list]
Think more about your next post so we don't have to play 20 questions to get the info we need to help you.

[/boilerplate_help_info]

commented: Not so bad (but needed to run the code jpg file etc they should attach) +13

@zeusgod: Ignore him.. he sometimes does that! Must probably be because he has been here for over 5 years. Maybe he doesn't know the code so he did the easiest thing to increase his posting. Anyways here's the code. And don't be pissed off because of walle he wants to keep things highly professional here. Professional means like guys with suits singing forum songs having a hundred rules with no respect to freedom. I had to make a software to change all my lolspeak terms to full-words just to post here without getting a warning! :P Tell me if you need it.

#include<stdio.h>
#define SIZE 50 // Maximum length of sentence
int main()
    {
        int i,j,pivot;
        char s[SIZE],temp[SIZE/2];
        printf("\nInput string: ");
        gets(s); // scanf won't read spaced words
        for(i=0,pivot=0;s[i]!='\0';i++) // loops till null character encountered
            {
                if(s[i]==32||s[i+1]=='\0') // 32 is ASCII value of space
                   {
                       for(j=i;j>=pivot;j--)
                       printf("%c",s[j]);
                       pivot=i+1;printf("%c",32);
                   }
            }
        return 0;
    }

One more thing, try reading C from the basics. A book like "Yashwant Kanetkar Let Us C (Indian author)" will help for the basics alone! Don't rely on this author for higher level. This program is pretty simple one. In fact, this code was done hurriedly but works fine, you can develop it into a shorter one later.! Shorter and better algorithm means quicker execution and lesser memory.

Use char array for storing the String & Use for loop up to null character & in that check the space character & print.

Maybe he doesn't know the code so he did the easiest thing to increase his posting.

I strongly doubt it. Walt is one of the better programmers on this forum.

Anyways here's the code.

And it doesn't solve the stated problem. But on the plus side, I can't scold you for giving away answers to obvious homework problems when your code is clearly unacceptable as a solution. :)

A book like "Yashwant Kanetkar Let Us C (Indian author)" will help for the basics alone!

A book is a good idea, but please recommend better books. Let Us C is utter crap.

Shorter and better algorithm means quicker execution and lesser memory.

Shorter and better are often mutually exclusive when it comes to algorithms. As an example, bubble sort is shorter, but quicksort is better.

Let's look at the code.

>#define SIZE 50 // Maximum length of sentence
That's a pretty short sentence.

>int main()
Kudos for using a correct definition of main().

>char s,temp[SIZE/2]; temp is unused.

>printf("\nInput string: ");
I never understood this whole newline at the beginning convention. It offers no benefits except a pointless blank line. A newline at the end makes more sense. Anyway, in this case you cannot guarantee that the prompt will be displayed before gets() blocks for input, so a call to fflush() is warranted:

printf("\nInput string: ");
fflush(stdout);

There are three times when the stream will be flushed:

  • When the stream's buffer is full
  • When a newline is displayed (in line oriented streams)
  • When fflush() is called

stdout is typically line oriented, so it's reasonably safe to assume that the stream will be flushed when you print a newline. However, in the case of user prompts it's not often desirable to jump to the next line, so the only option you have control over is an explicit call to fflush().

>gets(s); // scanf won't read spaced words
gets() is evil, there's no way to use it safely, stop using it and forget it exists. fgets() is the safe alternative, and it's wise to check input for failure:

if (fgets(s, sizeof s, stdin) != NULL) {
    /* ... */
}

>if(s==32||s[i+1]=='\0') { // 32 is ASCII value of space
Instead of making a comment, how about just saying what you mean?

if (s[i] == ' ' || s[i+1] == '\0') {

Or better yet, since words can be separated by whitespace characters other than ' ':

if (isspace(s[i]) || s[i+1] == '\0') {

Don't forget to include <ctype.h> for the declaration of isspace().

>printf("%c",s[j]);
printf() is pretty heavy handed for a single character. I'd suggest some variant of putchar().

>printf("%c",32);
Once again, say what you mean:

putchar(' ');

Here's my version of your code:

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

int main(void)
{
    char s[BUFSIZ];
    
    printf("Input string: ");
    fflush(stdout);
    
    if (fgets(s, sizeof s, stdin) != NULL) {
        int start = 0;
        int i;
        
        /* Strip the newline if present */
        s[strcspn(s, "\n")] = '\0';
        
        for (i = 0; s[i] != '\0'; i++) {
            if (isspace(s[i]) || s[i + 1] == '\0') {
                int j;
                
                for (j = i; j >= start; j--)
                    putchar(s[j]);
                    
                start = i + 1;
                putchar(' ');
            }
        }
        
        putchar('\n');
    }
    
    return 0;
}

And for the record, the problem was to reverse the words in a string, not the string itself. So "this is a test" would become "test a is this" rather than "tset a si siht". Your code only solves half of the problem.

A book is a good idea, but please recommend better books. Let Us C is utter crap.


can we take help with turbo c what do you think about it??

I ve gone through the book " LET US C " , there are many loop holes in the book, many wrong writings and incorrect programs in the book.(I am indian, so its a valid statement).
You can go through the sticky notes in the site and C Programming by K.N.KING is good book for beginner .
You can check this link .

Help from Turbo C is good, I personally learned a lot of things from there , but its very old compiler now and not used now.
some better option :-
Dev Cpp
Code Blocks
Visual C++
or any gcc compilers.

I ve gone through the book " LET US C " , there are many loop holes in the book, many wrong writings and incorrect programs in the book.(I am indian, so its a valid statement).
You can go through the sticky notes in the site and C Programming by K.N.KING is good book for beginner .
You can check this link .

Help from Turbo C is good, I personally learned a lot of things from there , but its very old compiler now and not used now.
some better option :-
Dev Cpp
Code Blocks
Visual C++
or any gcc compilers.

thanks a lot actually my teacher had recommended that book and i am searching for the new ones thanks again .can i ask you a favor please??:pretty:

@Yamna Hmmm...Sure... :)

@Yamna Hmmm...Sure... :)

actually i am working at our symister project and its getting so hard for my group so can you please help me personally to make that please

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.