C++ Array Question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Juggler
    New Member
    • Oct 2006
    • 6

    C++ Array Question

    I have a question about manipulating an array in C++. In my program, a cstring is entered. The string is printed. Finally, the string must be printed in reverse and displayed.

    The final part is where I am having my problem. I have the strlen function determine the legnth of the function, and I was then going manipulate the string by having the array go to the last character of the string entered, and then print each of the characters by cycling through the c-string backwards.

    I am having trouble with this. I can't seem to figure out how to get the strlen function to be the starting point of the array. Any suggestions?

    Thank you.
  • jessy
    New Member
    • Oct 2006
    • 106

    #2
    i hope i can help you with this simple example


    char *a ="jessy";
    cout<<" your string Reversed.."<<en dl;
    for ( int i=strlen(a);i >=0 ;i-- )
    {
    cout<<a[i]<<endl<<endl;
    try this code

    Comment

    • Juggler
      New Member
      • Oct 2006
      • 6

      #3
      Originally posted by jessy
      i hope i can help you with this simple example


      char *a ="jessy";
      cout<<" your string Reversed.."<<en dl;
      for ( int i=strlen(a);i >=0 ;i-- )
      {
      cout<<a[i]<<endl<<endl;
      try this code

      In char *a, what is the * for. I've never seen that before.

      Would this work though? The userInput is the original string. reversedUserInp ut is the the string that userInput is copied to. I get an error at the first line of code that I've attached.

      Code:
      strcpy(reversedUserInput, userInput, strlen(userInput));
      for (reversedUserInput; i >= 0; i--)
      {
        cout << "The string reversed:\n" << reversedUserInput << endl;                
      }
      Would this be close to be working? I can't seem to get what is wrong with the first line of code there.

      Comment

      • Juggler
        New Member
        • Oct 2006
        • 6

        #4
        Originally posted by Juggler
        In char *a, what is the * for. I've never seen that before.

        Would this work though? The userInput is the original string. reversedUserInp ut is the the string that userInput is copied to. I get an error at the first line of code that I've attached.

        Code:
        strcpy(reversedUserInput, userInput, strlen(userInput));
        for (reversedUserInput; i >= 0; i--)
        {
          cout << "The string reversed:\n" << reversedUserInput << endl;                
        }
        Would this be close to be working? I can't seem to get what is wrong with the first line of code there.
        Figured it out. strcpy should have been strncpy and then there were other random errors too.

        Comment

        • jessy
          New Member
          • Oct 2006
          • 106

          #5
          Good Luck !!

          BUt you stiLL dont't Know what the * is ????????

          this is POINTER ... haven' t you ever heared oF pointers Before ????!!!!!

          i guess they are very Important In learning c and c++
          so char *a is a pointer to a character array containing your string (user input)

          Comment

          Working...