English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

#include
#include
#include
int x,y;
char name[20],lo[20],up[20],rev[20];
main()
{
for(x=1;x<=1;x++)
{
cout<<"Enter your name:";
cin.getline(name,20);
y=name[x];
}
lo[x]=tolower(name[x]);
up[x]=toupper(name[x]);
rev[x]=name[y-x];
for(x=0;x<20;x++)
{
if(tolower(name[x]))
lo[x]=tolower(name[x]);
}
for(x=0;x<20;x++)
{
if(toupper(name[x]))
up[x]=toupper(name[x]);
}
for(x=1;x rev[x]=name[x-y];
cout<<"\nLower case: "< cout<<"\nUpper case is: "< cout<<"\nReverse is: "< getch();
}
What is wrong w/ my code the reverse character is not appear in my output.... Can you help me w/ this...

2007-03-17 02:52:34 · 3 answers · asked by Anonymous in Computers & Internet Programming & Design

I forgot to tell to all that w/o using strrev,strupr,strlwr

2007-03-17 20:55:06 · update #1

3 answers

This is an answer I already posted once under the page:
http://answers.yahoo.com/question/index;_ylt=AmX9M0v0QOWCnj0y0LZzkUEAAAAA?qid=20070224041134AAkBWvJ&show=7#profile-info-b7322d4e2697e617a2c6d2aebd0f6c42aa
I guess I'll repeat it again:

You're making it complex where it's simple! here is a better version of your code:

#include
#include
#include

using namespace std;

int main (void)
{
string str;
cout << "Enter a string\t\t=> ";
getline(cin, str);
cout << "The reversed string is\t=> ";
for (string::reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
cout << *i;
cout << endl;
system("pause");
return EXIT_SUCCESS;
}

If you are using C++ and not C, then you should use the C++ Standard String Library. In addition, your code is not ANSI C++ since it uses the Borland Conio Library; if you are under Windows, then simply you can use under the stdlib library the function system("pause") instead of getch().
Remember that now under ANSI C++ using namespace std is the preferred way to do things. Finally as you can see the String Library uses the advanced features of STL including the reverse iterators. In our case we used the reverse_iterator specifying it to to start the traversal from the rbegin (synonym for "start at the end of the string") and end till the beginning of the string. It's important to realize that the string is NOT yet really reversed, we only displayed it in reversed order.
To actually reverse it in such the indexes are really changed rather than just displaying it in reversed order, then perhaps this is one of the ways:

#include
#include
#include

using namespace std;

void reverse_string(string&);

int main (void)
{
string str;
cout << "Enter a string\t\t=> ";
getline(cin, str);
cout << "The reversed string is\t=> ";
reverse_string(str);
cout << str << endl;
system("pause");
return EXIT_SUCCESS;
}

void reverse_string(string& str)
{
string temp = "";
for (string::reverse_iterator i = str.rbegin(); i != str.rend(); ++i)
temp += *i;
str = temp;
return;
}

Hope it's helpful :-)

2007-03-17 03:13:12 · answer #1 · answered by Coosa 2 · 0 0

if you mean to reverse the string..like..if the string is "vishnu"..and the output is "unhsiv" then this is the code

char [20] string={"vishnu"}; //your char string
char temp//variable to store the character to be swapped
int length=strlen(string); // getting the lenght of your string
lenght--;// decrementing the lenght by one because your array starts from 0 to lenght-1
for(int i=0;i {
temp=string[i];//taking the char from left of the string
string[i]=string[lenght-i]//taking the char from the right of the string and putting it in the left of the string
string[lenght-i]=temp;//putting the char from the left of the string to the right of the string
}
cout<
for the above code to work...you'll have to include string.h header file..and in the for loop you'll notice that i have given the limit as lenght/2.... thats coz you are swapping the sting characters...and you just nedd to swap till the middle..!!!
i hope this clears your doubt..!!!

2007-03-17 03:41:51 · answer #2 · answered by ? 3 · 0 0

problem with ur code is b/c of y=name[x] & rev[x]=name[y-x]
here y means ascii value of charecter x in the string name.
suppose ur string is msc and so ur loop is runnig upto 1. so c is 1 and y=73 (ascii value of s) which is wrong.
.........................................................................................
for(x=1;x<=1;x++)
{
cout<<"Enter your name:";
cin.getline(name,20);
y=name[x];
}
..........................................................................................
i don't know why u keep in aloop it has no effect if there is no for also.
so u can run fallowing loop or simply call strrev function in string.h
int l=strlen(name)
for(int i=0;i rev[i]=name[l-i];//assign in reverse order
rev[i]='\0'; //to mark end of string

2007-03-17 05:59:51 · answer #3 · answered by m c 2 · 0 0

fedest.com, questions and answers