Thread: Coverting a string into upper or lowercase

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    Coverting a string into upper or lowercase

    Need some help with a program I am rying to work.

    I am trying to have a user input a string of characters, then count and compare the number of upper and lower case characters, then gie the option to change the string to all upper or all lower case letters, then display the changed string on the screen.


    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
           string x = "";
    	cout<<"Type your string: ";
    	cin>>x;
    
         return 0;
    
    }
    I have been completely stuck on this for the past 2 days.

    I was told by a friend that I needed to use ascii somehow? how?

  2. #2
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Use the functions isupper(), islower(), toupper(), and tolower() from ctype.h. They all accept a single character as a parameter.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  3. #3
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    I believe that if you use (char) in the toupper and tolower functions, you can input a string of text, too.

  4. #4
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    You can go toupper(97) that's the same as toupper('a') because 'a' is the ascii value of 97. http://www.asciitable.com/ for a decent ascii reference.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  5. #5
    Confused
    Join Date
    Nov 2002
    Location
    Warwick, UK
    Posts
    209
    Yes, but you can also do something like :

    Code:
    cout << (char)toupper((char)"hello");

    That's not right I think, but it's something like this, I tried it a few days ago.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    148
    Code:
    #include <iostream>
    #include <string>
    #include <algorithm>
    #include <functional>
    
    using namespace std;
    
    int main()
    {
       string str = "Hello World";
    
       transform(str.begin(),str.end(),str.begin(),ptr_fun(toupper));
    
       cout << str << endl;
    }

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    12

    Smile

    This works with Borland and MS Visual C
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    {   
        char* x ;
        int ucase=0;
        int lcase=0;
    	cout<<"Type your string: ";
    	cin.getline( x,100);
    	           
    	for(int i=0;i<strlen(x);i++)
    		{
    			if((x[i]>90)&&!(x[i]==32))
    				{
    		 		x[i]=x[i]-32;
    		        lcase++;
    		    	}
    		  	if(x[i]==32)ucase--;
    	     }
    	   
    	 ucase =strlen(x)-lcase+ucase; 
    	
    	 cout<<"The string you entered in uppercase was "<< x<<endl;
    	 cout<<"There were "<<ucase<<" uppercase letters in the string"<<endl;
    	 cout<<"There were "<<lcase<<" lowercase letters in the string"<<endl;
         getch();
         return 0;

  9. #9
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    bigwullie, you can use the ascii values instead of their decimal equivalents, ie:
    Code:
    char c;
    ...
    if (c >= 'a' && c <= 'z') // c is lowercase
    if (c >= 'A' && c <= 'Z') // c is uppercase
    else // c is not a letter
    It's also better to use <iostream> and <string> over their deprecated counterparts. You should also check out strings and why they are often preferred over char pointers.
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  10. #10
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by bigwullie
    This works with Borland and MS Visual C
    Code:
    #include <iostream.h>
    #include <string.h>
    #include <conio.h>
    
    int main()
    {   
        char* x ;
        int ucase=0;
        int lcase=0;
    	cout<<"Type your string: ";
    	cin.getline( x,100);
    	           
    	for(int i=0;i<strlen(x);i++)
    		{
    			if((x[i]>90)&&!(x[i]==32))
    				{
    		 		x[i]=x[i]-32;
    		        lcase++;
    		    	}
    		  	if(x[i]==32)ucase--;
    	     }
    	   
    	 ucase =strlen(x)-lcase+ucase; 
    	
    	 cout<<"The string you entered in uppercase was "<< x<<endl;
    	 cout<<"There were "<<ucase<<" uppercase letters in the string"<<endl;
    	 cout<<"There were "<<lcase<<" lowercase letters in the string"<<endl;
         getch();
         return 0;
    I'm suprised you didn't write your own strlen/output routine.

  11. #11
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    that helped, one more thing

    Ok I got the string to switch to upper or lower case, now what I need to do is insert a conditional statement asking if the user wants to change the case of the string after it reports the number of upper vs. lower case letters.

    [code]

    #include <cctype>
    #include <iostream>
    using namespace std;

    using std::cout;
    using std::endl;

    int main(void)
    {
    char sent[] = "";
    char* x=0;
    int ucase=0;
    int lcase=0;
    cout << "Input Your String: " << sent << endl;
    cin>> sent;

    for(int i=0;i<strlen(sent);i++){

    if((sent[i]>90)&&!(sent[i]==32)){
    sent[i]=sent[i]-32;
    lcase++;
    }
    if(sent[i]==32)ucase--;
    }

    ucase =strlen(sent)-lcase+ucase;

    cout<<"There were "<<ucase<<" uppercase letters in the string"<<endl;
    cout<<"There were "<<lcase<<" lowercase letters in the string"<<endl;
    cout<<"Change the case of the string? (Y or N)" <<endl;

    // does the conditional statemnt go here? or before the cout above?

    for (char *iter = sent; *iter != '\0'; ++iter)
    {
    *iter = tolower(*iter);
    ++iter;
    }

    cout << "Here is your converted string: " << sent << endl;

    return 0;
    }

  12. #12
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    You started out on the right foot using string, I'm not quite sure why you reverted to using character arrays and not using functions which are already provided for you in cctype.
    Code:
    string s;
    int upper = 0, lower = 0;
    char c;
    cin >> s;
    for (string::iterator i = s.begin(); i != s.end(); ++i)
    {
              if (isupper(*i))
                        ++upper;
              else if (islower(*i))
                        ++lower;
    }
    cout << "upper: " << upper << '\n';
    cout << "lower: " << lower << '\n';
    cout << "convert to lowercase? ";
    cin >> c
    if (c == 'y' || c == 'Y')
              // convert string to lowercase
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

  13. #13
    Registered User
    Join Date
    Jul 2003
    Posts
    9

    Grrrr... almost got it

    almost....

    I got it to accept a command to change the string to the users choice, but when I choose to change to all lowercase I still get all uppercase

    Here it is so far:

    Code:
    #include <cctype> 
    #include <iostream> 
    using namespace std;
    
    using std::cout;
    using std::endl;
    
    int main(void)
    {
    	char sent[] = "";
    	char* x=0;
    	char c=0;
    	int ucase=0;
    	int lcase=0;
    		cout << "Input Your String: " << sent << endl;
    		cin>> sent;
    
    	for(int i=0;i<strlen(sent);i++){
    
    		if((sent[i]>90)&&!(sent[i]==32)){
    			sent[i]=sent[i]-32;
    			lcase++;
    }
    		if(sent[i]==32)ucase--;
    }
    
    	ucase =strlen(sent)-lcase+ucase; 
    
    		cout<<"There were "<<ucase<<" uppercase letters in the string"<<endl;
    		cout<<"There were "<<lcase<<" lowercase letters in the string"<<endl;
    		cout<<"Change the case of the string to upper or lower? (U or L)" <<endl;
    		cin>>c;
    		if (c == 'u' || c == 'l'){
    		}
    		cout<<sent;
    
    
    return 0; 
    }
    HELP!! hehe

  14. #14
    Registered User slaveofthenet's Avatar
    Join Date
    Apr 2003
    Posts
    80
    Your code is riddled with errors. Since you put using namespace std; at the top, you don't need using std::cout or using std::endl. You don't properly allocate space for the string. The main loop is converting all of the character to uppercase, and the if statement at the bottom is doing nothing.
    Code:
    #include <iostream>
    #include <cctype>
    #include <string>
    #include <algorithm>
    
    using namespace std;
    
    int main()
    {
    	string s;
    	int lower = 0, upper = 0;
    	char c;
    	cout << "enter string: ";
    	cin >> s;
    	for (string::iterator i = s.begin(); i != s.end(); ++i)
    	{
    		if (islower(*i)) ++lower;
    		else if (isupper(*i)) ++upper;
    	}
    	cout << "upper: " << upper << '\n';
    	cout << "lower: " << lower << '\n';
    	cout << "convert to upper or lower case (u/l)? ";
    	cin >> c;
    	if (c == 'l' || c == 'L')
    		transform(s.begin(), s.end(), s.begin(), tolower);
    	else if (c == 'u' || c == 'U')
    		transform(s.begin(), s.end(), s.begin(), toupper);
    	cout << s;
    	return 0;
    }
    Detailed understanding of language features - even of all features of a language - cannot compensate for lack of an overall view of the language and the fundamental techniques for using it. - Bjarne Stroustrup

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  3. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  4. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM