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 <cctype>

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++)

any suggestion?

Recommended Answers

All 9 Replies

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

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.

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 :)

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 <iostream>
#include <cstring>
#include <cctype>

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;   
}

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

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.

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;
}

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.