Thread: string functions?

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    42

    string functions?

    Is there a function to reverse the order of a string?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    strrev
    "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

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Thank you.

  4. #4
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    If you have strrev, otherwise you can write your own really easily. This one is independent of any header files, meaning you don't need string.h :-)
    Code:
    #include <stdio.h>
    
    static void revstr(char *s)
    {
      char *start;
      char *end;
    
      /* Find the end */
      for (end = s; *end != '\0'; end++)
      {
        /* Do nothing */
      }
    
      /* Reverse the string */
      for (start = s, --end; start < end; start++, end--)
      {
        char temp = *start;
        *start = *end;
        *end = temp;
      }
    }
    
    int main(void)
    {
      char s[] = "A string";
    
      revstr(s);
      printf("%s\n", s);
    
      return 0;
    }
    Or you can use strlen and get rid of the first explicit loop :-)
    Code:
    #include <string.h>
    
    static void revstr(char *s)
    {
      char *start;
      char *end;
    
      /* Find the end */
      end = s + (strlen(s) - 1);
    
      /* Reverse the string */
      for (start = s; start < end; start++, end--)
      {
        char temp = *start;
        *start = *end;
        *end = temp;
      }
    }
    *Cela*

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    42
    Thank you.

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Here's another way of doing it:

    Code:
    #include <stdio.h>
    
    void reverse(char *str)
    {
        int i,j=strlen(str)-1;
        for(i=0;i<j;++i,--j)
    	str[i] ^= str[j] ^= str[i] ^= str[j];
    }
    
    
    int main() 
    {
        char *s = "This is a test string.";
    
        printf("%s\n", s);
        reverse(s);
        printf("%s\n", s);
    }

  7. #7
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Code:
    /* #include <stdio.h> 
        #include <string.h>
    */
    
    void reverse(char *str)
    {
        /* strlen returns size_t but at times I use int */
        int i,j=strlen(str)-1;
        for(i=0;i<j;++i,--j)
                    /*  http://www.eskimo.com/~scs/C-faq/q3.8.html */
    	str[i] ^= str[j] ^= str[i] ^= str[j];
    }
    
    
    int main() 
    {
        /* On some machines stores the string in read only memory */
        char *s = "This is a test string.";
    
        printf("%s\n", s);
        reverse(s);
        printf("%s\n", s);
    
        /* main returns an integer value */
        /* return 0; */
    }

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: 8
    Last Post: 04-25-2008, 02:45 PM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. Badly designed n string functions?
    By anonytmouse in forum C Programming
    Replies: 3
    Last Post: 11-01-2003, 06:16 AM
  5. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM