G
gambler
Guest
Ok so here's the thing i've written two snippets for a program that is supposed to ask the user to input a string then prints the string backwards, It's also supposed to convert all uppercase letters to lowercase and all lowercase letters to uppercase. Any idea, how I can make this whole? (I've been trying for days!!!)
Here's the reversing part:
#include <iostream>
#include <string>
const int LINE_SIZE = 20;
using namespace std;
int main()
{
string correct_word;
char temp_word[LINE_SIZE+1];
int i, j;
cout << "Please enter word to be reversed: ";
cout.flush();
cin >> correct_word;
cin >>
/* correct for the '\0' from the read string */
j = correct_word.size() -1;
for(i = 0; j >= 0; )
temp_word[i++] = correct_word[j--];
/* make sure reversed word isn't ending in garbadge */
temp_word = '\0';
string reverse_word(temp_word);
cout << "Read word was: " << correct_word << endl;
cout << "Reversed word: " << reverse_word << endl;
return 0;
}
And here's the snippet for converting the "cases":
char ch;
cout<<"Enter a character: ";
cin>> ch;
if ( isalpha ( ch ) ) {
if ( isupper ( ch ) )
cout<<"The lower case equivalent is "<< static_cast<char> ( tolower ( ch ) ) <<endl;
else
cout<<"The upper case equivalent is "<< static_cast<char> ( toupper ( ch ) ) <<endl;
}
else
cout<<"The character is not a letter"<<endl;
// Flush the input stream
while ( cin.get ( ch ) && ch != '\n' )
;
cin.get();
I'm having trouble figuring out how I could go through each position of the string and reverse it then check it's position before I output it~ get my drift? Any help will do! Thanks
Here's the reversing part:
#include <iostream>
#include <string>
const int LINE_SIZE = 20;
using namespace std;
int main()
{
string correct_word;
char temp_word[LINE_SIZE+1];
int i, j;
cout << "Please enter word to be reversed: ";
cout.flush();
cin >> correct_word;
cin >>
/* correct for the '\0' from the read string */
j = correct_word.size() -1;
for(i = 0; j >= 0; )
temp_word[i++] = correct_word[j--];
/* make sure reversed word isn't ending in garbadge */
temp_word = '\0';
string reverse_word(temp_word);
cout << "Read word was: " << correct_word << endl;
cout << "Reversed word: " << reverse_word << endl;
return 0;
}
And here's the snippet for converting the "cases":
char ch;
cout<<"Enter a character: ";
cin>> ch;
if ( isalpha ( ch ) ) {
if ( isupper ( ch ) )
cout<<"The lower case equivalent is "<< static_cast<char> ( tolower ( ch ) ) <<endl;
else
cout<<"The upper case equivalent is "<< static_cast<char> ( toupper ( ch ) ) <<endl;
}
else
cout<<"The character is not a letter"<<endl;
// Flush the input stream
while ( cin.get ( ch ) && ch != '\n' )
;
cin.get();
I'm having trouble figuring out how I could go through each position of the string and reverse it then check it's position before I output it~ get my drift? Any help will do! Thanks