Hi everybody,

I'm new in programming. Getting started with C. Im trying to do a few easy things like to uppercase a string.

But i got stuck...

This is what i have so far:

void toupper( char *text )
{
     char c[] = "testING"; 
     
}

And im not sure if this is good..

Can somebody help me with this function please?

Thnx in advance!

iterate through each character of the string and use the macro toupper() found in ctype.h to convert the characters to upper case.

char c = 'a';
c = toupper(c);
// now c will == 'A'

Thank you for your reply Ancient Dragon's!

This was what i was looking for:

void convertToUpperCase(char *sPtr)
{
      while(*sPtr != '\0')
      {
         if (islower(*sPtr))
              *sPtr = toupper(*sPtr);
       }
}

Got it working now :D

if (islower(*sPtr))
  *sPtr = toupper(*sPtr);

There's no need for islower because toupper will convert the character as requested, or do nothing if there's no conversion or if the character is already upper case. In other words, toupper "just does the right thing". However, because your function can't guarantee that *sPtr is within the range that toupper expects (an integer in the range of an unsigned char), you should cast it to unsigned char before passing it to toupper:

void convertToUpperCase(char *sPtr)
{
  while(*sPtr != '\0')
    *sPtr = toupper((unsigned char)*sPtr);
}
if (islower(*sPtr))
  *sPtr = toupper(*sPtr);

However, because your function can't guarantee that *sPtr is within the range that toupper expects (an integer in the range of an unsigned char), you should cast it to unsigned char before passing it to toupper:

void convertToUpperCase(char *sPtr)
{
  while(*sPtr != '\0')
    *sPtr = toupper((unsigned char)*sPtr);
}

What do you mean by "your function can't guarantee that *sPtr is within the range that toupper expects (an integer in the range of an unsigned char)" ?

I didn't use any int's. Im a newbie in programming so bare with me please, but im willing and motivated to learn it.

So could you explain that please?

Thnx in advance!

>What do you mean by "your function can't guarantee that *sPtr is
>within the range that toupper expects
toupper is declared like so:

int toupper ( int c );

Even though it takes an integer as the argument, the value of c is expected to be within the range of an unsigned char, that is, 0 to 255 for 8-bit chars. The reason for this requirement is that toupper is usually nothing more than an array index, and if c has a value outside of that range, bad things happen.

Let's say you pass -5 to toupper, what happens? It depends on the implementation, but the majority of the time, the implementation looks like this:

int toupper ( int c )
{
  return utbl[c];
}

If c is -5 and utbl is (as expected) defined as:

unsigned char utbl[256] = {
  /* Conversions from lower case to upper case */
};

Then you would be accessing utbl[-5], which has undefined behavior. If the implementation chooses, it can add a corrective statement to cover for this:

int toupper ( int c )
{
  if ( (unsigned int)c > 255 )
    return c;
  return utbl[c];
}

But that's *not* required, and you can't rely on it. Your function has no way at all to tell if *sPtr is a character within that range without extra tests, so you force it to be. At least then your program will be debuggable whereas it could otherwise mysteriously break and you would have no idea where to start searching for the problem.

>I didn't use any int's.
Sure you did, just not explicitly. When you pass *sPtr to toupper, it's promoted to an int.

Thnx for your reply Narue!

They both don't work.

This is what i tried to convert to uppercase:

main(){
char text[] = "TessSSttTTinGGG";

printf("output = %s", text );
uppercase( text)
}

And this is my function:

int uppercase( char *sPtr )
{
    /while( *sPtr != '\0' )
      {
         *sPtr = toupper( ( unsigned char ) *sPtr );
      }
}

Shouldn't this be working like this?

Thnx in advance!

As posted, it won't work.
It needs a semicolon after the uppercase function.

void main(){
char text[] = "TessSSttTTinGGG";

printf("output = %s", text );
uppercase(text)
printf("Output = %s", text); //text now has all entries in uppercase.
}
int uppercase( char *sPtr )
{
    while( *sPtr != '\0' )
      {
         *sPtr = toupper( ( unsigned char ) *sPtr );
      }
}

You may also have problems because you aren't returning an int value. You can either change uppercase to return void, or you can put in a return 1 or 0 or 500 (it's arbitrary, unless you want to add some sort of error-checking ability to the function).

>They both don't work.
Do you go to the doctor and say "I don't feel good", and expect an accurate diagnosis?

>They both don't work.
Do you go to the doctor and say "I don't feel good", and expect an accurate diagnosis?

Probably yes, but then again, im just a 12 year old kid :sad:

>Probably yes, but then again, im just a 12 year old kid
Then I'll be more specific. "It doesn't work" is useless to us because there are so very many ways for something to "not work". We want details, such as error messages, output with an explanation of what's not right, any input including file contents, how you call the program, the compiler switches you use, your operating system, your compiler, even your shoe size might help. Hell, I wouldn't say no to a disassembly and core dump if you have it. ;)

My point is that the more information you give us, the better a position we're in to help you find and solve the problem.

>Probably yes, but then again, im just a 12 year old kid
Then I'll be more specific. "It doesn't work" is useless to us because there are so very many ways for something to "not work". We want details, such as error messages, output with an explanation of what's not right, any input including file contents, how you call the program, the compiler switches you use, your operating system, your compiler, even your shoe size might help. Hell, I wouldn't say no to a disassembly and core dump if you have it. ;)

My point is that the more information you give us, the better a position we're in to help you find and solve the problem.

Ow i see.... More details, sorry about that!

Well it doesn't give any errors. I can run it, but everything keeps black in the dos prompt.

My pc runs on windows XP pro. And im using dev-C++ 4.9.9.2

All im using right now is that uppercase() function.

Thnx in advance!

>I can run it, but everything keeps black in the dos prompt.
Ah, I made the same mistake in the code I posted. Serves me right for being in a hurry. ;) You need to increment sPtr in the uppercase function or the loop will just go on forever. Here's the fixed and cleaned up code:

#include <stdio.h>

int uppercase ( char *sPtr )
{
  while ( *sPtr != '\0' ) {
    *sPtr = toupper ( ( unsigned char ) *sPtr );
    ++sPtr;
  }
}

int main ( void )
{
  char text[] = "TessSSttTTinGGG";

  printf ( "Output = %s\n", text );
  uppercase ( text );
  printf ( "Output = %s\n", text );

  getchar(); /* Pause for Dev-C++ */

  return 0;
}

>I can run it, but everything keeps black in the dos prompt.
Ah, I made the same mistake in the code I posted. Serves me right for being in a hurry. ;) You need to increment sPtr in the uppercase function or the loop will just go on forever. Here's the fixed and cleaned up code:

#include <stdio.h>

int uppercase ( char *sPtr )
{
  while ( *sPtr != '\0' ) {
    *sPtr = toupper ( ( unsigned char ) *sPtr );
    ++sPtr;
  }
}

int main ( void )
{
  char text[] = "TessSSttTTinGGG";

  printf ( "Output = %s\n", text );
  uppercase ( text );
  printf ( "Output = %s\n", text );

  getchar(); /* Pause for Dev-C++ */

  return 0;
}

Great, It works! :cheesy:

Thank you Narue for your help and patience!!!!!!! :)

Here is better way to write this function because declaring the integer function without return value is illegal in C99. So, it is better to declare void.

void uppercase(char *sPtr) {  
    while(*sPtr != '\0') {
         *sPtr++ = toupper(*sPtr);
    }
}

Hello, ashihs2expert. If you try to compile your snippet, it gives the following: warning: operation on ‘sPtr’ may be undefined [-Wsequence-point] on line 3, because the precedence of the operators '*' and '++' is undefined. That´s why its better to do:

void uppercase ( char *sPtr )
{
    while ( *sPtr != '\0' )
    {
    *sPtr = toupper ( ( unsigned char ) *sPtr );
    ++sPtr;
    }
}

changing the prototype of the function as you suggested. Cheers

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.