Thread: Reversing Letters in Words of a Sentence

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    2

    Reversing Letters in Words of a Sentence

    I'm having trouble reversing the letters of words in a sentence, for instance:

    Input: This is a test
    Output: sihT si a tset

    However, when I run the program and have the same input as above, my output is just the first word reversed. Here is my code:

    Code:
     #include<stdio.h>
     #include<string.h>
     
     void rev_str(char *string, size_t length)
     {
      	int start, end;
    
      	for (start = 0, end = length-1; start < end; start++, end--)
      	{
        	char swap = string[start];
        	string[start] = string[end];
        	string[end] = swap;
      	}
     }
    
     char *rev_words(char *str)
     {
      	int length;
      	char *p = str, *next;
    
      	for (p = str; p != 0; p = strpbrk(p, " "))
      	{
        	if (*p == ' ')
       		{
          		p++;
       		}
    
        	next = strpbrk(p, " ");
    		length = (next != 0) ? next - p : &str[strlen(str)] - p;
    		rev_str(p, length);
     	}
      	return str;
     }
    
     int main(void)
     {
    	char str[100];
    
    	printf("Enter your sentence:");
    	scanf("%s", str);
      	printf("%s\n", rev_words(str));
    
      	return 0;
     }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    For starters, %s stops at whitespace... Fix how you read your input first.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    2
    Either I'm misunderstanding you, or it isn't doing much for me. With the adjustment I made it doesn't even print the first word reversed.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What adjustment have you made? You'll note that I didn't say I fixed your other problems. Rather, I said "fix how you read your input first". In other words, it won't matter if you try to fix your other problem, because if you aren't reading your input right in the first place, you won't be able to know if you're on the right track.

    "Garbage in, garbage out", as the saying goes...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by quzah View Post
    For starters, %s stops at whitespace... Fix how you read your input first.

    Yea try "%[^\n]\n" is stead of "%s"

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Last edited by quzah; 03-31-2009 at 03:54 PM. Reason: WTFAQ?
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Really it's your sscanf() that needs to be inside a loop.
    This way the sentence is reversed a word at a time.
    The way it is setup now it'll stop after reading the first word.
    Last edited by itCbitC; 03-31-2009 at 05:03 PM.

  8. #8
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by quzah View Post
    Why not use fgets?

    Cprogramming.com: FAQ

    Quzah.

    It's more work to make that change.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by esbo View Post
    It's more work to make that change.
    Seriously?
    Code:
    fgets( buf, sizeof buf, stdin );
    There, it's working.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by quzah View Post
    Seriously?
    Code:
    fgets( buf, sizeof buf, stdin );
    There, it's working.


    Quzah.
    Yea I would say it is involved about 4-10 times as much work.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The difference is that I can break your program with input, and you can't break mine. How much "work" is it to figure out why I'm crashing your program?

    On a side note, str is a reserved keyword, as is anything that starts with str.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Begginer Problem: Extracting words from sentence
    By barlas in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2006, 03:17 PM
  2. New Theme
    By XSquared in forum A Brief History of Cprogramming.com
    Replies: 160
    Last Post: 04-01-2004, 08:00 PM
  3. How to count the words in sentence ?
    By Th3-SeA in forum C Programming
    Replies: 1
    Last Post: 10-01-2003, 01:34 AM
  4. Searching for words within a sentence
    By drdroid in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:09 AM