|
-
Jan 31st, 2001, 12:56 PM
#1
Thread Starter
Frenzied Member
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
-
Jan 31st, 2001, 03:32 PM
#2
PowerPoster
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.
-
Feb 1st, 2001, 04:05 AM
#3
Hyperactive Member
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
===========================
-
Feb 1st, 2001, 04:17 AM
#4
Frenzied Member
Are you sure it's "String" and not "string" ?
Harry.
"From one thing, know ten thousand things."
-
Feb 1st, 2001, 02:21 PM
#5
Hyperactive Member
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
===========================
-
Feb 1st, 2001, 02:57 PM
#6
Frenzied Member
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."
-
Feb 1st, 2001, 03:02 PM
#7
Monday Morning Lunatic
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 << x << 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
-
Feb 1st, 2001, 03:12 PM
#8
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 << x << endl;
}
-
Feb 1st, 2001, 03:15 PM
#9
Monday Morning Lunatic
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
-
Feb 1st, 2001, 03:17 PM
#10
Frenzied Member
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."
-
Feb 1st, 2001, 03:21 PM
#11
Thread Starter
Frenzied Member
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;
}
-
Feb 1st, 2001, 03:21 PM
#12
Monday Morning Lunatic
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
-
Feb 1st, 2001, 03:36 PM
#13
PowerPoster
Thank you Vlatko! That was all I needed.
-
Feb 1st, 2001, 07:31 PM
#14
Hyperactive Member
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
===========================
-
Feb 1st, 2001, 08:19 PM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|