Thread: reversing a string, again..

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    4

    reversing a string, again..

    after browsing through the search function for most of the day, and still having trouble with reversing string, I figured its time for this noob to ask for some help.

    I'm working on a assignment where I have to reverse the words, and characters of an array, without using strrev. If the user inputs 'have a nice day' the output should be

    yad
    ecin
    a
    evah

    Reversing the words I can do. Just can't wrap my head around copying the last character of an array to the first character of another. Any help wound be greatly appreciated.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    
    
    int main(int argc, char* argv[])
    {
    
            char words[3][10], reversed[3][10], temp[3][10];
            int count, num, length;
    
            for(count=0; count<3; count++)
            {
                    printf("\nPlease enter word #%d: ",count+1);
                    gets(words[count]);
    
            }
            puts("");
    
    
            length=strlen(words);
    
    
            for(count=2; count>-1; count--)
            {
                    for(num=0; num<length/2; num++)
                    {
                            strcpy(reversed[num],words[length-1]);
                    }
    
                    //puts(words[count]);
                    puts(reversed[num]);
    
            }
    
    
            getch();
            return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't use strcpy to copy anything other than a string. Specifically, you don't use strcpy to copy a character; you use = to copy a character.

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    87
    Also, using gets() is not advised. From the manual page on my system:

    BUGS
    Never use gets(). Because it is impossible to tell without knowing the
    data in advance how many characters gets() will read, and because
    gets() will continue to store characters past the end of the buffer, it
    is extremely dangerous to use. It has been used to break computer
    security. Use fgets() instead.

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    4
    So to copy the characters of the array into a char variable, like this, but I get an invalid indirection error. Not sure what I'm doing wrong.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include<string.h>
    
    
    int main(int argc, char* argv[])
    {
    
            char words[3][10], reversed[3][10], temp[3][10], charCopy;
            int count, num, length;
    
            for(count=0; count<3; count++)
            {
                    printf("\nPlease enter word #%d: ",count+1);
                    gets(words[count]);
            }
            puts("");
    
            length=strlen(words);
    
                    for(count=0, num=length-1; count<num; count++, num--)
                    {
                            charCopy[count] = words[num];
                    }
    
    
                    //puts(words[count]);
                    printf("%c",charCopy);
    
            getch();
            return 0;
    }

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
            length=strlen(words);
    What are you trying to do there? Get it to return the length of all the words at once? That's not going to work. If you want each one's length, you need to check each word one at a time.

    Also, look at what your'e doing here:
    Code:
    charCopy[count] = words[num];
    Again you're looking at a whole word, not each letter of the word. I would probably advice against figuring out how to handle all the different words for now. Just try to get it working with reversing one word.

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

  6. #6
    Registered User
    Join Date
    Jun 2009
    Location
    undisclosed
    Posts
    5
    pseudo code for reversing strings
    Code:
    LOOP{
        new_array[ first_index ] = old_array[ last_index ]
    
        increase first_index count,
        decrease last_index count,
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. Reversing a String
    By ToasterPas in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2001, 12:20 PM