954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

new to c++ __coverting uppercase to lowercase vice versa

Write a program that will prompt the user to type in a series of characters. Input these characters into a c-string (char array). For each character, if it is a letter, toggle the case (upper to lower case, or lower to upper case). All other characters leave as is. Print out the string before and after.

and

I i've been use:

#include

and

islower(), isupper(), tolower(), and toupper(), and maybe isalpha(). to the help the coding.

In the for loop, use this format:
for (i=0; arrayname[i]; i++)

any suggestion?

d1s_0n3_guy
Newbie Poster
6 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

What have you done so far? We don't solve homeworks

Sci@phy
Posting Whiz in Training
279 posts since Sep 2008
Reputation Points: 110
Solved Threads: 43
 

Yes, you've collected all the tools you'll need. Now put them together in some actual code.

I kind of like your for loop - clever way you end it. Goes against my feeling that for loops should be controlled by the value of the counter, but it's more succinct than the equivalent while loop implementation.

vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 
I kind of like your for loop - clever way you end it.

As do I. Which automatically makes me believe that it's part of the assignment given by the teacher :)

Nick Evan
Not a Llama
Moderator
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
 
As do I. Which automatically makes me believe that it's part of the assignment given by the teacher :)


okay i think i got it all together ..check this out..
and it is an assignment.... i was just asking for any input on it...
but here is my code and for the next guy who wants to use it .. i don't mind... in the end its all up to you weather you learn it or not...you might learn something new from it... do what ever you want with it.....


#include
#include
#include

using namespace std;

int main ()
{
char letters[20];
char temp;
int i;
cout << "Pease enter characters " << endl;
cin >> letters;
for (i=0; letters[i]; i++)
{
if (isalpha(letters[i]))

{if (islower(letters[i]))
temp=toupper(letters[i]);
if (isupper(letters[i]))
temp=tolower(letters[i]);
cout << temp;
}
else
{temp=letters[i];
cout << temp;
}
}
cout << endl;
cin >> i;

return 0;
}

d1s_0n3_guy
Newbie Poster
6 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

and how could i make it better? fancier or what ever.. i like to get a good grasp on this...

d1s_0n3_guy
Newbie Poster
6 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Here's code from something I was doing the other-night:

string str = "I'm a fairy! abc xyz ABC XYZ";
int casechanged = 0;

cout << str;
    for(i = 0; i < str.length(); i++)
    {
          if(isUpper(str[i])) { str[i] = toLower(str[i]); casechanged = 1; }
          if(isLower(str[i]) && casechanged != 1) { str[i] = toUpper(str[i]);}
          
          casechanged = 0;
    }
    cout << str;
    
    for(i = 0; i < str.length(); i++)
    {
          if(isLower(str[i])) { str[i] = toUpper(str[i]); casechanged = 1; }
          if(isUpper(str[i]) && casechanged != 1) { str[i] = toLower(str[i]); }
          
          casechanged = 0;
    }
   cout << str;

The isLower/Upper(), and toUpper/toLower(), are my own functions I wrote, just replace them with the proper names of the ones you're using.

MosaicFuneral
Posting Virtuoso
1,691 posts since Nov 2008
Reputation Points: 888
Solved Threads: 116
 

koool thanx

d1s_0n3_guy
Newbie Poster
6 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

Here's your code, cleaned up and simplified a bit.

When posting code here, please use the tags
[code]
your code here
[/code]
This will better preserve your indenting, making the code easier to read.

You should adopt a consistent style of indenting, and use of curly braces. You're mixing styles, which hinders readability. Also use some blank spaces in a line between operators and operands. You're not charged by the byte.

#include <iostream>
#include <cctype>

using namespace std;

int main ()
{
	char letters[20];
	int i;

	cout << "Pease enter characters " << endl;
	cin >> letters;

	cout << letters << endl; //assignment said display the original

	for ( i = 0; letters[i]; i++ )
	{
		if ( isalpha(letters[i] ) )
		{
			if ( islower(letters[i] ) )
				letters[i] = (char) toupper( letters[i] );
			else //we know it's a letter, and not lowercase
				letters[i] = (char) tolower( letters[i] );
		}
		//don't do anything to non-alpha
	}

	//display the whole string, not one char at a time in the loop
	cout << letters << endl;  //the changed version

	cout << endl;

	return 0;
}
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
 

thank you ..it does look a lot better ....

d1s_0n3_guy
Newbie Poster
6 posts since Nov 2008
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You