1,105,450 Community Members

Function to convert a lower case string into upper case

Member Avatar
nychick
Newbie Poster
14 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
1
 

Hi guys, I'm trying to write a function that could convert a lowercase string to uppercase. Not sure if I'm heading the right direction.
Essentially the function

ToUpperString (char s[]);

would convert everything in the string to the uppercase equivalent.

Here's what I got so far:

void ToUpperString ( char s[] )
{
  static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static char lower[] = "abcdefghijklmnopqrstuvwxyz";

  for ( int i = 0; lower[i] != '\0'; i++ ) {
    if ( ch == lower[i] )
      return upper[i];
  }

  return char s[];
}

Please tell me if I'm going the right direction, any help is greatly appreciated.

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

Use toupper function in cctype header file.

Member Avatar
nychick
Newbie Poster
14 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks but I can't use any libraries ;(
Must write on my own.

Member Avatar
firstPerson
Industrious Poster
4,052 posts since Dec 2008
Reputation Points: 761 [?]
Q&As Helped to Solve: 634 [?]
Skill Endorsements: 24 [?]
 
0
 

Ok then, some comments.

Where is ch declared in : ch == lower

Why are you returning in a void function?


Make a toUpper fuction, that takes in a char and returns the upper cased of it.

So for example :

cout << toUpper('a') << endl; //displays 'A';
cout << toUpper('A') << endl; //displays 'a';

Then you can just use a loop for a string to upper it.

string str = "upperThis";
for(int i = 0; i < str.size(); i++)
     str[i] = toUpper(str[i]);
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

Take a look at an ASCII table: http://web.cs.mun.ca/~michael/c/ascii-table.html how do the two cases relate?

Member Avatar
nychick
Newbie Poster
14 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Take a look at an ASCII table: http://web.cs.mun.ca/~michael/c/ascii-table.html how do the two cases relate?

oh by 0x20

Member Avatar
nychick
Newbie Poster
14 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

so can I just use | 0x20 to convert the caps?

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 
char c = 'A';
char d = c ^ 0x20;  //'a'
char e = d ^ 0x20; //'A' again

The regular OR only works in one direction(from upper to lower)
also you could just add or subtract 32 from the char if it's easier.

Member Avatar
nychick
Newbie Poster
14 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I tried to use the more mechanical method but my syntax with arrays is wrong. How can I fix this?

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

bool is_upper ( char s[] )
{
  static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

  for ( int i = 0; upper[i] != '\0'; i++ ) {
    if ( s[] == upper[i] )
      return true;
  }

  return false;
}

bool is_lower ( char s[] )
{
  static char lower[] = "abcdefghijklmnopqrstuvwxyz";

  for ( int i = 0; lower[i] != '\0'; i++ ) {
    if ( s[] == lower[i] )
      return true;
  }

  return false;
}

char to_lower ( char s[] )
{
  static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static char lower[] = "abcdefghijklmnopqrstuvwxyz";

  for ( int i = 0; upper[i] != '\0'; i++ ) {
    if ( s[] == upper[i] )
      return lower[i];
  }

  return s[];
}

char to_upper ( char s[] )
{
  static char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  static char lower[] = "abcdefghijklmnopqrstuvwxyz";

  for ( int i = 0; lower[i] != '\0'; i++ ) {
    if ( s[] == lower[i] )
      return upper[i];
  }

  return s[];
}

int main()
{
  char s[80];

  cout<<"Enter a word: ";
  cin>> s[];

  if ( is_upper ( s[] ) )
    cout<<"The lower case equivalent is "<< to_lower ( s[] ) <<endl;
  else if ( is_lower ( s[] ) )
    cout<<"The upper case equivalent is "<< to_upper ( s[] ) <<endl;
  else
    cout<<"The character is not a letter"<<endl;

}
Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

First grab a peek at cin.get() http://www.cplusplus.com/reference/iostream/istream/get/ to see how to get those characters into that char array.

Secondly, you have to make some design decisions. Will your functions process the characters (which in your code they must devine on their own with no indicies) being fed into them one by one or the whole string? I think you want to choose one by one, it keeps the messy array from bouncing around and if you wanted to put in just 'A' to your functions one day you can.

So, use a for loop to march through your string in main, sending the chars (and chars alone) to your functions, so s. Next, clear out the s[] junk from the functions and just treat it for a char s. Compare it with your string and once it matches return the upper or lowercase character only.

Back in main I did this

for (int i = 0;s[i] !='\0';i++) 
 {
	if ( is_upper (s[i]) )
            cout <<s[i]<<"The lowercase equivalent is: "<<to_lower ( s[i] )<<endl;
        //your other fun here
}

so displaying it char by char next to the original. Change that up if you need it to meet your spec.

Member Avatar
nychick
Newbie Poster
14 posts since Dec 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

First grab a peek at cin.get() http://www.cplusplus.com/reference/iostream/istream/get/ to see how to get those characters into that char array.

Secondly, you have to make some design decisions. Will your functions process the characters (which in your code they must devine on their own with no indicies) being fed into them one by one or the whole string? I think you want to choose one by one, it keeps the messy array from bouncing around and if you wanted to put in just 'A' to your functions one day you can.

So, use a for loop to march through your string in main, sending the chars (and chars alone) to your functions, so s. Next, clear out the s[] junk from the functions and just treat it for a char s. Compare it with your string and once it matches return the upper or lowercase character only.

Back in main I did this

for (int i = 0;s[i] !='\0';i++) 
 {
	if ( is_upper (s[i]) )
            cout <<s[i]<<"The lowercase equivalent is: "<<to_lower ( s[i] )<<endl;
        //your other fun here
}

so displaying it char by char next to the original. Change that up if you need it to meet your spec.

Thanks, I read the cin.get() function but still don't quite get it.

I guess that I can't just insert the array right into the function like what I did.

bool is_upper ( char s[] )

How can I fix that by using cin.get() ?
Thanks

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 

I was talking about you using cin >> in the beginning. It is incorrect. cin.get(s,80) is how you get your characters into the array and has nothing to do with the functions.

I'm saying write your functions as if they were for testing one character at a time. Send in the character, your "telephone book" is scanned for it, return which ever results necessary. In the main program, your loop will then send in another and repeat.

As you had it, sending in s[] was incorrect, since if you were sending in the entire array it would be just s. The s[] is in the declaration and definition of the function only.

Member Avatar
stevee1984
Newbie Poster
13 posts since Nov 2009
Reputation Points: 0 [?]
Q&As Helped to Solve: 3 [?]
Skill Endorsements: 0 [?]
 
0
 
void MyUpper(char * text) {
	if (text) {
		for (int i = 0;text[i] !='\0';i++) {
			if (text[i] >= 'a' && text[i] <= 'z') {
				text[i] -= ('a' - 'A');
			}
		}
	}
}

Alternative approach?

Member Avatar
jonsca
Quantitative Phrenologist
4,273 posts since Sep 2009
Reputation Points: 1,059 [?]
Q&As Helped to Solve: 607 [?]
Skill Endorsements: 14 [?]
Team Colleague
Featured
Sponsor
 
0
 


Alternative approach?

We tried to start OP down that path but she preferred this method (closer to class requirement?)

Question Answered as of 4 Years Ago by jonsca, firstPerson and stevee1984
Member Avatar
Rogierownage
Newbie Poster
2 posts since May 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

By the way, this

s[i] !='\0'

can be shortened to this

s[i]

Because a null value is equal to false, and every other char value is equal to true.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article