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?

Dani AI

Generated

Good start — you already have the right tools: <cctype> functions plus a loop will do the job. A few practical points that will make the program more robust and more useful later on (and tie together suggestions from and ):

  • Avoid fixed-size char buffers and cin >> (it stops at whitespace and risks overflow). Use std::string and std::getline so full lines (spaces included) are handled safely.
  • Always call the C locale character functions with unsigned char cast: e.g. std::isalpha(static_cast<unsigned char>(ch)). Passing a plain char that is negative is undefined behavior on some platforms.
  • std::toupper/std::tolower return int; cast the result back to char when storing.
  • If you ever need non-ASCII letter handling, consider locale-aware conversion (std::ctype facet) or a Unicode library — the C char functions only cover the C locale/ASCII reliably.

Example (simple, safe toggle that prints before/after):

#include <iostream>
#include <string>
#include <cctype>

int main() {
    std::string orig;
    std::getline(std::cin, orig);                // read whole line
    std::string out; out.reserve(orig.size());

    for (char ch : orig) {
        unsigned char uc = static_cast<unsigned char>(ch);
        if (std::isalpha(uc))
            out.push_back(static_cast<char>(std::islower(uc) ? std::toupper(uc) : std::tolower(uc)));
        else
            out.push_back(ch);
    }

    std::cout << "Before: " << orig << "\nAfter:  " << out << '\n';
}

Extra tips: the little ASCII bit-trick (xor 0x20) flips case only for plain ASCII letters — it’s fast but non-portable and unsafe for full text. For simple homework use the standard functions with the unsigned char cast. This respects ’s original intent, follows ’s cleanup advice about showing the original string, and keeps ’s idea of using helper checks but leans on the standard library for correctness.

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.

koool thanx

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.