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.

Use toupper function in cctype header file.

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

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]);
commented: i think this code is more appropriate but could you care to put the whole code snippet and i could test how it runs +0

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

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.

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;

}

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.

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

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 for stevee1984
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?


Alternative approach?

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

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.

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.