Printing my name in reverse

This is a discussion on Printing my name in reverse within the C++ Programming forums, part of the General Programming Boards category; Could anyone please tell me how I can print my name in reverse? Thanks #pragma argsused int main(int argc, char* ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    71

    Printing my name in reverse

    Could anyone please tell me how I can print my name in reverse?

    Thanks

    #pragma argsused
    int main(int argc, char* argv[])

    {
    char myString[] = {"Jason"};
    int i = 0;

    while ( myString[i] != '\0' )
    {
    cout << myString[i] << "";
    i++;
    }
    getch();
    return 0;
    }
    Code:
    /________________________________________________________________

  2. #2
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Re: Printing my name in reverse

    Originally posted by Guti14
    Could anyone please tell me how I can print my name in reverse?

    Thanks

    #pragma argsused
    int main(int argc, char* argv[])

    {
    char myString[] = {"Jason"};
    int i = 0;

    while ( myString[i] != '\0' )
    {
    cout << myString[i] << "";
    i++;
    }
    getch();
    return 0;
    }
    Code:
    /________________________________________________________________



    how abt

    Code:
    int main(int argc, char* argv[])
    
    {
    char myString[] = {"Jason"};
    int i = 0;
    
    while ( myString[i] != '\0' )
    {
    i++;
    }
    
    for(int j=i-1;j>=0;j--)
    cout<<cout << myString[j] << "";
    
    
    getch();
    return 0;
    }

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,673
    Just for some spice...
    Code:
    #include <string>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    
    int main()
    {
        string name("Jason");
        reverse_copy(name.begin(),name.end(),ostream_iterator<char>(cout,""));
        cout << endl;
        return 0;
    }
    Outputs:
    nosaJ

    Mileage may vary as to what additional headers you may need to include (maybe <iterator>?), these worked as is under MSVC++ 6.0.
    I used to be an adventurer like you... then I took an arrow to the knee.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Code:
    #include <iostream>
    #include <string.h>
    using namespace std;
    int main()
    {
        char name[] = "jason";
        int len = strlen(name) - 1;
        while ( len >= 0)
            cout << name[len--];
            
        cout << endl;
        system("PAUSE");
    }

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    #include <string.h>

    should be

    #include <cstring>

  6. #6
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by 7stud
    #include <string.h>

    should be

    #include <cstring>
    In MSVC++ 6.0 (AFAIK), it isn't fully ANSI/C++ complient.

  7. #7
    Registered User
    Join Date
    Jun 2002
    Posts
    230
    Well from what ive been told and from hte new c++ standards, but correct me if im wrong.

    Code:
     
    #include <string.h>
    
    should be
    
    #include <string>
    from what i have heard string is now standard
    C++ Rules!!!!
    ------------
    Microsoft Visual Studio .NET Enterprise

  8. #8
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    I though CString was a mfc thing and not a part of standard c++.

    #include <string> gives access to the string class and make it possible to create a string variable and use it's methods.

    I also thought that when using #include <string.h> you get "access" to the old c-string methods, like strlen.

  9. #9
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    To use the c-style string manipulation functions, you include <cstring>. I believe the MFC header is <cstring.h>
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. Problem with my reverse function and its output!
    By Matus in forum C Programming
    Replies: 4
    Last Post: 04-29-2008, 08:33 PM
  3. gethostbyaddr() reverse lookups failing (???)
    By Uncle Rico in forum C Programming
    Replies: 9
    Last Post: 08-19-2005, 09:22 AM
  4. Printing name in reverse order and in caps
    By Guti14 in forum C++ Programming
    Replies: 1
    Last Post: 08-17-2003, 07:02 AM
  5. Printing String in reverse order
    By ling in forum C Programming
    Replies: 14
    Last Post: 10-18-2001, 09:03 AM

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