C++ arrays help (new to C++ will be easy question)

Discussion in 'Programming' started by Gjubi, Mar 31, 2008.

  1. #1
    I am currently in a C++ beginners class and i have an assignment that i need to make. where i create a program and read in from a .txt file a bunch of word such as pig, car, cat. And with these words i need to use arrays and c strings to make a program that will read in the words and take the words, reverse the letters aswell as show the words in opposite order.

    ex: PIG
    CAT
    DOG
    HOUSE

    will print out as:

    ESUOH
    GOD
    TAC
    GIP

    i hope you understand the type of program i need to create, and also if you wish to help me design the code please use simple techniques which a beginner would understand.

    thanks gjubi.
     
    Gjubi, Mar 31, 2008 IP
  2. Aphex

    Aphex Guest

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    
    char *ReverseWord(char WordToReverse[])
    {
        int i, j; //j = size of WordToReverse
        char temp; //temp holder for WordToReverse
    
        for (i = 0, j = strlen(WordToReverse) - 1; i <= j; i++, j--)//for statement
        {
            temp = WordToReverse[i];
            WordToReverse[i] = WordToReverse[j]; //move WordToReverse[j] into WordToReverse[i]
            WordToReverse[j] = temp; //move temp into WordToReverse[j]
        }
    
        return WordToReverse; //return the word in reverse order
    }
    
    //USAGE:
    char *szReversed - ReverseWord("HOUSE"); //returns ESUOH
    
    Code (markup):
    This what you mean?
     
    Aphex, Mar 31, 2008 IP
  3. Gjubi

    Gjubi Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    WOW yes i think this really helped it seems to look alot like what i have been learning in class, i just was unable to grasp the concept as to how to reverse the arrays. no matter what i thought nothing made sense. but thanks!
     
    Gjubi, Apr 1, 2008 IP
  4. it career

    it career Notable Member

    Messages:
    3,562
    Likes Received:
    155
    Best Answers:
    0
    Trophy Points:
    270
    #4
    Move strlen(WordToReverse) out of for loop.
     
    it career, Apr 2, 2008 IP
  5. Kuriyaki

    Kuriyaki Peon

    Messages:
    257
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    When you need to create programs to learn concepts...I'd strongly advise to stay away from solutions. They will only hinder your learning as simple concepts such as arrays, reading in, etc will affect you later on.

    I'd ask for tips on how to start.
     
    Kuriyaki, Apr 2, 2008 IP
  6. Gjubi

    Gjubi Peon

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I totally understand what you mean its like im getting someone to do it for me. But i read the C++ book and listen well in class, but i feel that if i can see how the code needs to be written i understand it way better because i never know like what are the exact words i need to write. and it seems that if i see and example that does roughly what i need i can understand it way better and see how everything comes together, i think its just the complicated way that i learn the best.
     
    Gjubi, Apr 2, 2008 IP