Thread: rotate string

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    7

    Question rotate string

    We want to read a string (max 20 characters) and display it turned around.
    So ABCD becomes DCBA!
    This should not be difficult, but we tried several things in C, but all didn't work....!!

    Please Help!!!

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    What'd you try?

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    We try to Convert a string:
    From ABCD --> DCBA

    We allready tried this:

    #include "..\ownh.h"
    #include "..\stdio.h"

    int main (void)
    {
    /* Programm */
    char mystring [10] ;
    int waarde1 = 10;
    char uitk [10];
    float mystr [10];
    int waarde2;
    printf ("Fill in string value: ");
    scanf ("%s",mystring);
    printf ("String value is: %s\n",mystring);

    while (waarde1 > 0)
    {
    printf ("Converted: %s\n",mystring+waarde1);

    waarde1 = waarde1-1;
    }
    printf ("%s",uitk);

    pause ();
    return 0;
    }

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    ...well gee, did you try the strrev function.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Code:
    void stringturn(char *str)
    {
      unsigned int front,back;
      char f,b;
    
      back=strlen(str)-1; /* for the strlen() function you must include string.h */
      for(front=0;front!=back;front++, back--)
      {
        f=*(str+front);
        b=*(str+back);
    
        f^=b;
        b^=f;
        f^=b;
      }
    }
    klausi
    When I close my eyes nobody can see me...

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >...well gee, did you try the strrev function.
    strrev isn't a standard function, he may not have it.

    Code:
    void stringturn(char *str)
    {
      unsigned int front,back;
      char f,b;
    
      back=strlen(str)-1; /* for the strlen() function you must include string.h */
      for(front=0;front!=back;front++, back--)
      {
        f=*(str+front);
        b=*(str+back);
    
        f^=b;
        b^=f;
        f^=b;
      }
    }
    Oh yuck, not only does this not work, that XOR trick is really very nasty and should be avoided. Try something along these lines instead:
    Code:
    /* Pseudocode */
    static void revstr ( char *a )
    {
      char b[BUFSIZ] = {0};
      int s = 0, e = strlen ( a ) - 1;
      while ( e >= 0 )
        b[s++] = a[e--];
      strcpy ( a, b );
    }
    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    7

    Question

    How do I have to use the strrev option???????

  8. #8
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Thumbs up

    If you want to use the strrev function then you should add the #include <string.h> at the begging of your .c file. The use for the strrev is "strrev (string);".
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Thank you, Prelude for correcting me, but why is my "XOR-trick" "nasty"?
    I used it to save the memory for a temporary variable.

    Maybe that works:
    Code:
    char *stringturn(char *str)
    {
      unsigned int front,back;
    
      back=strlen(str)-1; /* for the strlen() function you must include string.h */
      for(front=0;front<=back;front++, back--)
      {
        *(str+front)^=(str+back);
        *(str+back)^=(str+front);
        *(str+front)^=(str+back);
      }
    
      return str;
    }
    klausi
    Last edited by klausi; 03-30-2002 at 10:32 AM.
    When I close my eyes nobody can see me...

  10. #10
    Registered User
    Join Date
    Mar 2002
    Posts
    7
    Well, thanks for the help,
    We will try it tomorrow in school...
    We just started programming in C one week ago...

    When we have new problems we'll post them :-)

    -----
    Workgroup CIT Aventus

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > When we have new problems we'll post them :-)

    NO! Didn't you read.... oh of course you didn't. Silly me. Don't just post your problem, unless you've made an effort on your own, and post your try (IE: code) with your question.

    If you get errors that you're working on, post the exact error. We are not the homework committee.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM