473,385 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

uppercase/lowercase swap function

Pavel
2
My task was to write a program that reads keyboard input to EOF and that echoes the input except for digits, converting each uppercase character to lowercase, and vice versa.

#include <iostream.h>
#include <ctype.h>
int main(void)
{char ch;
while ((ch=cin.get())!='$')
{ if (islower(ch)) toupper(ch);
else if (isupper(ch)) tolower(ch);
if (isalpha(ch) || isspace(ch) || ispunct(ch))
cout <<ch;}
cin.get();
cin.get();
return 0;
}

Please help me out to work out why toupper( ) and tolower( ) functions are not executed?
Jan 5 '07 #1
6 5924
r035198x
13,262 8TB
My task was to write a program that reads keyboard input to EOF and that echoes the input except for digits, converting each uppercase character to lowercase, and vice versa.

#include <iostream.h>
#include <ctype.h>
int main(void)
{char ch;
while ((ch=cin.get())!='$')
{ if (islower(ch)) toupper(ch);
else if (isupper(ch)) tolower(ch);
if (isalpha(ch) || isspace(ch) || ispunct(ch))
cout <<ch;}
cin.get();
cin.get();
return 0;
}

Please help me out to work out why toupper( ) and tolower( ) functions are not executed?
I'm curious to know what you got as output there. It seems you cout only if (isalpha(ch) || isspace(ch) || ispunct(ch))

you should indent your code probably and make good use of { }

And also use code tags when posting code.
Jan 5 '07 #2
DeMan
1,806 1GB
I'm not entirely familiar with those functions, but I would imagine that toupper RETURNS a char (and same for tolower) so yoiu should have

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <ctype.h>
  3.  
  4. int main(void)
  5. {
  6.   char ch;
  7.   while ((ch=cin.get())!='$')
  8.   { 
  9.     if (islower(ch)) 
  10.       ch = toupper(ch);
  11.     else if (isupper(ch)) 
  12.       ch = tolower(ch);
  13.     if (isalpha(ch) || isspace(ch) || ispunct(ch)) 
  14.       cout <<ch;
  15.   }
  16.   cin.get();
  17.   cin.get();
  18.   return 0;
  19. }
  20.  
Jan 5 '07 #3
DeMan
1,806 1GB
and with
Expand|Select|Wrap|Line Numbers
  1. isalpha(ch)
  2.  
alphabetic characters are output.......?
Jan 5 '07 #4
r035198x
13,262 8TB
I'm not entirely familiar with those functions, but I would imagine that toupper RETURNS a char (and same for tolower) so yoiu should have

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <ctype.h>
  3.  
  4. int main(void)
  5. {
  6. char ch;
  7. while ((ch=cin.get())!='$')
  8. if (islower(ch)) 
  9. ch = toupper(ch);
  10. else if (isupper(ch)) 
  11. ch = tolower(ch);
  12. if (isalpha(ch) || isspace(ch) || ispunct(ch)) 
  13. cout <<ch;
  14. }
  15. cin.get();
  16. cin.get();
  17. return 0;
  18. }
  19.  
You are right. This is probably what the OP intended
Jan 5 '07 #5
Ganon11
3,652 Expert 2GB
Also, you won't have to check for spaces or punctuation. You should just be able to output each character after you switch from uppercase to lowercase. The toupper() and tolower() functions actually return an integer variable (the ASCII number representing that char), so you will have to cast the result of these functions to a character (static_cast<char>(toupper(ch));).

Also, the toupper() and tolower() functions only return something different if the character given is lowercase or uppercase, respectively. If you pass it a space, or a backslash, for example, the function will return the ASCII value of a space or backslash, since neither of these count as a letter and, therefore, cannot be uppercase or lowercase.
Jan 5 '07 #6
Pavel
2
I'm not entirely familiar with those functions, but I would imagine that toupper RETURNS a char (and same for tolower) so yoiu should have

Expand|Select|Wrap|Line Numbers
  1. #include <iostream.h>
  2. #include <ctype.h>
  3.  
  4. int main(void)
  5. {
  6.   char ch;
  7.   while ((ch=cin.get())!='$')
  8.   { 
  9.     if (islower(ch)) 
  10.       ch = toupper(ch);
  11.     else if (isupper(ch)) 
  12.       ch = tolower(ch);
  13.     if (isalpha(ch) || isspace(ch) || ispunct(ch)) 
  14.       cout <<ch;
  15.   }
  16.   cin.get();
  17.   cin.get();
  18.   return 0;
  19. }
  20.  
Thank you very much it works quite well, I forget a simple assignment
Jan 13 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

23
by: Hallvard B Furuseth | last post by:
Has someone got a Python routine or module which converts Unicode strings to lowercase (or uppercase)? What I actually need to do is to compare a number of strings in a case-insensitive manner,...
2
by: brendan | last post by:
Hi, I need to get replace all the uppercase characters in a string with a lowercase character and a '-' __ie __________________________________________________ 'backgroundColor' becomes...
8
by: Markus Dehmann | last post by:
My ios::uppercase (and others) seems not to work. #include <iostream> using namespace std; int main(){ int num = 45; cout.setf(ios::hex | ios::showbase | ios::uppercase); cout << "number " <<...
1
by: The_Kingpin | last post by:
Hi all, I need to make a function that convert a string into a certain format. Here what are the restriction: -The first letter of the first and last name must be uppercase. -If a first name...
6
by: BSHELTON | last post by:
How do I convert existing lowercase data to uppercase in Access 2000? I used the following which did not work? UPDATE HousingTowns SET tMunis=UPPER(tMunis); "HousingTowns" is the table name....
2
by: t8ntboy | last post by:
I have a table the contains field called PersonID. Each record in the personID field begins with the letter "p" in uppercase or lowercase. I want to run a query that finds all of the lowercase...
4
by: titan nyquist | last post by:
Why does ToTitleCase not work if the case is already in upper case? I have to make my string lowercase, first, before I pass to to ToTitleCase to have it work. Titan
4
by: jerger | last post by:
i have a great program now with the help of a member from this site, but i need a little customization to meet the needs of non-english speakers... who might accidendtly type punctuation which would...
9
by: humaid | last post by:
hi guys,i have this problem.first let me show you the so far code #!/user/bin/perl while ($a = <STDIN>) { if ($a =~ tr/A-Z/a-z/) { print "$a\n"; }
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.