Thread: manual string reverse

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    28

    manual string reverse

    enlighten me with my mistake

    Code:
    #include<stdio.h>
    
    int str_rev(char *s1)
    {
      char str2[50];
      char *s2=str2;
      int i=0,len=0;
      while(*(s1+i) != '\0')
         i++;
      i--;
      while(i!=0)
         {
            *(s2+len)=*(s1+i);
            i--;
            len++;
         }
    // *(s2+len)='\0';
    //  printf("\nreversed string == > %s",s2);
      while(*(s2+i) != '\0')
         {
            *(s1+i)=*(s2+i);
         }
      *(s1+i)='\0';
      printf("\nreversed string 2 ==> %s\n",s1);
      return 0;
    }
    
    int main()
    {
     char str[20];
     char *s1 = str;
     printf("\nEnter string\n");
     scanf("%s",s1);
     str_rev(s1);
     return 0;
    }

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    It's a lot easier to track down a problem when we know what the problem is - are you getting compiler errors? runtime errors? And if so, what do the errors say and where do they occur? Is the program running but just outputting incorrect output? And if so, what are your test inputs / outputs?

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    i'm getting an incorrect output.... no errors... sorry for not specifying these things in my first post...
    Code:
    ./a.out
    
    Enter string
    abcd
    
    
    
    dfkj
    dsaf
    asf
    nkl
    ^Z
    [1]+  Stopped

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    all the characters in the output screen are what i typed

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
     char str[20];
     char *s1 = str;
     printf("\nEnter string\n");
     scanf("%s",s1);
     str_rev(s1);
    You don't have to do that, you know. You can use the name of an array by itself to treat it as a pointer (to the first element of the array). In fact, "array" is just like "&array[0]".
    Code:
    char str[20];
    scanf("%s", str);
    Also, fgets() is better suited to user input than scanf("%s") . . . SourceForge.net: Scanf woes - cpwiki

    Finally, there are easier ways to reverse a string. Consider this pseudo-code:
    Code:
    set start to start of string
    set end to point to the end of the string
    while start < end:
        swap *start and *end
        increment start, decrement end
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    thanks for ur time mate

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    As to what could be going wrong *(s2+i) != '\0' doesn't ever have to be true, since you never terminate the s2 string....

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, not to mention that i is never changed in this loop, so it's probably infinite.
    Code:
      while(*(s2+i) != '\0')
         {
            *(s1+i)=*(s2+i);
         }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    still.... i'm not able to find my error.....

    ***incremented the value of i in the while loop**** that was very silly from my side

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you'd said at the beginning "it hangs when I run it," then we'd have known immediately to look for an infinite loop and helped you that much faster. So why don't you tell us what it does now . . . ?

    Incidentally, this loop
    Code:
      while(i!=0)
         {
            *(s2+len)=*(s1+i);
            i--;
            len++;
         }
    won't process *(s1 + 0). So the first element of the string probably won't be copied.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    28
    the pseudo code helped me.... thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM