Returning a string

This is a discussion on Returning a string within the C Programming forums, part of the General Programming Boards category; I need to reverse a string and display the original using one function. I have the function to reverse it, ...

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    1

    Returning a string

    I need to reverse a string and display the original using one function. I have the function to reverse it, but i dont know how to display the original one. Any help would be appreciated.

    Code i have so far:

    Code:
    #include <string.h>
    #include <stdio.h>
    #define CAPACITY 35
    
    
    void reverseString(char *str)
    int main (void)
    {
        char first[CAPACITY];
        char last[CAPACITY];
        printf(" Enter your first and last name: ");
        scanf("%s %s", first, last);
        reverseString(first, first);
        reverseString(last, last);
        printf("The reverse of your name is: %s,%s \n", first, last);
    }
    
    
    void reverseString(char *str)
    {
        char *p = str;
        char *q = str + strlen(str) - 1;
        char temp;
        
        while(p<q)
        {
            temp = *p;
            *p = *q;
            *q = temp;
            ++p;
            --q;
        }    
    }
    Last edited by poly; 11-16-2012 at 04:26 PM.

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    Your function works by overwriting the original string, so before calling your function you can either:
    1) print the original string
    2) make a copy of the original string to print later (see strcpy() in strings.h)

  3. #3
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <string.h>
    
    static char *rev(char *to, const char *from)
    {
        const char *endp = from + strlen(from);
        char *save = to;
    
        while (endp > from)
            *to++ = *--endp;
        *to = '\0';
        return save;
    }
    
    static int read(char *to, size_t n)
    {
        int c;
    
        n--;
        while ((c = getchar()) != EOF && !isspace(c))
            if (n) {
                *to++ = c;
                n--;
            }
        *to = '\0';
        return c;
    }
    
    int main(void)
    {
        char first[35], last[35];
        char rfirst[sizeof first], rlast[sizeof last];
    
        while (read(first, sizeof first) != EOF
         && read(last, sizeof last) != EOF)
            printf("%s %s\n", rev(rfirst, first), rev(rlast, last));
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 09-06-2011, 02:59 PM
  2. Replies: 10
    Last Post: 12-03-2010, 11:04 PM
  3. Returning a string...
    By Vber in forum C Programming
    Replies: 4
    Last Post: 12-04-2002, 05:46 PM
  4. returning a string by value??
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-02-2002, 10:41 AM
  5. returning std::string
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2001, 08:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21