Results 1 to 15 of 15

Thread: Reverse String

  1. #1

    Thread Starter
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Code:
    _strrev, _wcsrev, _mbsrev
    Reverse characters of a string.
    
    char *_strrev( char *string );
    
    wchar_t *_wcsrev( wchar_t *string );
    
    unsigned char *_mbsrev( unsigned char *string );
    
    Routine Required Header Compatibility 
    _strrev <string.h> Win 95, Win NT 
    _wcsrev <string.h> or <wchar.h> Win 95, Win NT 
    _mbsrev <mbstring.h> Win 95, Win NT 
    
    
    For additional compatibility information, see Compatibility in the Introduction.
    
    Libraries
    
    LIBC.LIB Single thread static library, retail version 
    LIBCMT.LIB Multithread static library, retail version 
    MSVCRT.LIB Import library for MSVCRT.DLL, retail version 
    
    
    Return Value
    
    Each of these functions returns a pointer to the altered string. No return value is reserved to indicate an error.
    
    Parameter
    
    string
    
    Null-terminated string to reverse
    
    Remarks
    
    The _strrev function reverses the order of the characters in string. The terminating null character remains in place. _wcsrev and _mbsrev are wide-character and multibyte-character versions of _strrev. The arguments and return value of _wcsrev are wide-character strings; those of _mbsrev are multibyte-character strings. For _mbsrev, the order of bytes in each multibyte character in string is not changed. These three functions behave identically otherwise. 
    
    Generic-Text Routine Mappings
    
    TCHAR.H Routine  _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined 
    _tcsrev _strrev  _mbsrev  _wcsrev  
    
    
    Example
    
    /* STRREV.C: This program checks an input string to
     * see whether it is a palindrome: that is, whether
     * it reads the same forward and backward.
     */
    
    #include <string.h>
    #include <stdio.h>
    
    void main( void )
    {
       char string[100];
       int result;
    
       printf( "Input a string and I will tell you if it is a palindrome:\n" );
       gets( string );
    
       /* Reverse string and compare (ignore case): */
       result = _stricmp( string, _strrev( _strdup( string ) ) );
       if( result == 0 )
          printf( "The string \"%s\" is a palindrome\n\n", string );
       else
          printf( "The string \"%s\" is not a palindrome\n\n", string );
    }
    
    
    Output
    
    Input a string and I will tell you if it is a palindrome:
    Able was I ere I saw Elba
    The string "Able was I ere I saw Elba" is a palindrome
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  2. #2
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    That is some very nice code, but can I print the string reversed to the screen? And can I use cin and cout? I am doing this for class, and we haven't learned how to use the printf statement.
    <removed by admin>

  3. #3
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Wink

    Well talking about the strings, I have came across a problem.

    I am reading a book on C++ and one of the programs is

    #include<iostream.h>
    #include<string.h>

    int main()
    {
    String Name;
    cout<< "Enter your Name: ";
    cin>> Name;
    and so on...
    return 0;
    }

    and when I compile the program, the compiler doesn't recognize the String and tells me it is undeclared identifier. Anybody haves any idea why this happens?
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Are you sure it's "String" and not "string" ?
    Harry.

    "From one thing, know ten thousand things."

  5. #5
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256

    Unhappy I am pretty sure!

    Originally posted by HarryW
    Are you sure it's "String" and not "string" ?
    Hi, Yeah I am pretty sure it is String. Although instead of string.h I have tried other libraries but i get the same errors. I tried:
    string.h
    String.h
    wchar.h
    mbstring.h

    But non of them works. I have absoloutly no clue why this happen.

    P.S. I know I have all the libraries in my include folder.

    Any guesses??

    Thanks in advance
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  6. #6
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Okay, try

    #include <string>

    without the .h at the end. <string> is the STL string library (the ANSI standard), <string.h> is Microsoft's version. In <string> there should be a class called 'string'. I think it's all lower case, but I'm not certain.
    Harry.

    "From one thing, know ten thousand things."

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    string.h
    A set of functions for manipulating chararacter arrays.

    string
    Part of the STL -- includes the string class.

    To use string:
    PHP Code:
    #include <iostream> // You must use this version!
    #include <string>

    void main() {
        
    string x;
        
    cin >> x;

        
    cout << << endl;

    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  8. #8
    Guest
    You seem to have left out something important.....

    PHP Code:
    #include <iostream> // You must use this version!
    #include <string>
    using namespace std//this is needed!
    void main() {
        
    string x;
        
    cin >> x;

        
    cout << << endl;


  9. #9
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Oops....yep. I did miss it - thanks Dennis

    I suppose this is what happens when you're at 39 degrees ....damn I'm not thinking straight
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  10. #10
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Why must he use the ANSI version? Why is it so essential?

    is it because <string> won't work with <iostream.h> ?
    Harry.

    "From one thing, know ten thousand things."

  11. #11

    Thread Starter
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    This post was simply on how to reverse a string
    Code:
    #include <iostream.h>
    #include <windows.h>
    
    int main() 
    { 	
    char str[] = "Vlatko";
    cout<<strrev(str)<<endl; 
    return 0; 
    }
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yes.

    Well...it will work with it, but you can't do cin >> string with it.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  13. #13
    PowerPoster MidgetsBro's Avatar
    Join Date
    Oct 2000
    Location
    Apparently, Internet.com
    Posts
    3,125
    Thank you Vlatko! That was all I needed.
    <removed by admin>

  14. #14
    Hyperactive Member kourosh's Avatar
    Join Date
    Aug 1999
    Location
    Vancouver, British Columbia, Canada
    Posts
    256
    Originally posted by denniswrenn


    PHP Code:
    using namespace std//this is needed! 
    Thanks alot to everybody, although I had one more question what does
    using namespace std; do??


    Thanks again.
    ===========================
    Kourosh Gonabadi
    VB Programmer
    C++ Newbie
    Graphic Designer
    ===========================

  15. #15
    Guest
    it "uses" the std namespace...

    ex:

    PHP Code:
    #include <iostream>

    int main()
    {
    std::cout << "test";
    return 
    0;

    if theres only a few cout's, that isn't so bad is it?

    but if theres about 50... well, look how messy this is...

    PHP Code:
    #include <iostream>
    #include <string>

    int main()
    {
        
    std::string str
        std
    ::cout << "enter a string" << std::endl;
        
    std::cin >> str;
        
    std::cout << str << " is the value of str" << std::endl;
        return 
    0;

    if you use using namespace std you don't need to use std::

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width