How to transform char or string to uppercase

Hello, I can't find a function that allows me to do this, I always seem to get an error with every one I use(call to undefined function).. I'm using Borland 5.02, is there a specific one that's used only in this. I greatly appreciate any help... Thanks!
Last edited on
closed account (E0p9LyTq)
If you have a C-style ANSI string you can use the tolower() or toupper() functions in the C library header <cctype>, towlower() and towupper() if you have wide characters.
http://www.cplusplus.com/reference/cctype/
http://www.cplusplus.com/reference/cwctype/

http://www.cplusplus.com/reference/cctype/tolower/ (with example)

For a C++ string there are several different options:
http://stackoverflow.com/questions/735204/convert-a-string-in-c-to-upper-case
Thanks! I used this code and it worked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <ctype.h>
#include <conio>
#include <iostream>

int main()
{
   int i = 0;
   char str[100];
   cin.getline(str,100);

   while(str[i])
   {
      putchar (toupper(str[i]));
      i++;
   }
   getch();
   return 0;
}


However if I do it without the while loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>
#include <ctype.h>
#include <conio>
#include <iostream>

int main()
{
   char str[100];
   cin.getline(str,100);

cout<<toupper(str[100]);

   getch();
   return 0;
}


It just returns the number 88.. what could I be doing wrong here?
Last edited on
Well, I just get an "X" now regardless of what I write.. could this be a problem specific to Borland?
It successfully changed one single letter to uppercase lol

Thanks for patience with me btw
The functions toupper/tolower works on one character at a time. If you want to transform all characters in a string you would have to use a loop.
aloz,

The first bit of code worked fine in Visual Studio 2015. Since the code is simple c code I do not think that Borland would be your problem. The while loop works because toupper() and tolower() work on a single character not a string of characters. the putchar() changes the int returned by toupper() to an unsigned char before it is output to the screen.

I do not know why you only were able to get one character to change, but maybe you could step through the while loop to see what happens with each iteration.

Hope that helps you,

Andy
Topic archived. No new replies allowed.