Thread: to Reverse the words in the string... help.. I just can't see the error...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    The process can be as simple or complex as you'd like. For instance, a simpler but more difficult to understand method would be to use two pointers and walk down the string. When the end pointer finds a space or the end of the string, reverse from the start pointer to the end pointer and assign the end pointer to the start pointer. Lather, rinse, repeat.
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    static void revstr ( char *start, char *end )
    {
      char hold;
    
      while ( start < end ) {
        hold = *start;
        *start = *end;
        *end = hold;
    
        start++;
        end--;
      }
    }
    
    int main ( void )
    {
      char buf[] =  "Hello world haha";
      char *last = buf + sizeof buf;
      char *start = buf;
      char *end;
    
      for ( end = start; end != last; start = ++end ) {
        while ( *end != '\0' && !isspace ( *end ) )
          end++;
    
        revstr ( start, end - 1 );
      }
    
      printf ( "%s\n", buf );
    
      return 0;
    }
    -Prelude
    Last edited by Prelude; 03-18-2003 at 08:24 PM.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  2. How to monitor process creation?
    By markiz in forum Windows Programming
    Replies: 31
    Last Post: 03-17-2008, 02:39 PM
  3. more then 100errors in header
    By hallo007 in forum Windows Programming
    Replies: 20
    Last Post: 05-13-2007, 08:26 AM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM