lower to upper

Pages: 12
Dec 22, 2010 at 6:47pm
i wanna to make a prog which convert small case letters entered by user to upper case letters....i TRIED to use toupper function ...but it only do action with a single character...is there any function which convert all characters like "lovely hakeem" to "LOVELY HAKEEM"
(NOT BY LOOP)

plz help
Dec 22, 2010 at 6:55pm
There is no way to convert all characters to upper or lower case without looping through the string.
Dec 22, 2010 at 7:03pm
actually some one told me that it is possible.....for that reason
Dec 22, 2010 at 7:13pm
You could use std::for_each():

1
2
std::string str = "lower";
std::for_each(str.begin(), str.end(), std::toupper);

Technically, though, std::for_each() is still looping through the string.
Dec 22, 2010 at 7:15pm
Using a string you can use std::transform.

http://www.cplusplus.com/reference/algorithm/transform/
Dec 22, 2010 at 7:16pm
ok i will tryyy.tnxxxx
Dec 22, 2010 at 7:17pm
it mean that there is no keyword for it????
Dec 22, 2010 at 7:21pm
That's not keyword material. If you want a simple function that will do the job, just write one:

1
2
3
4
void toUpper(std::string& s)
{
    std::for_each(s.begin(), s.end(), std::toupper);
}
Last edited on Dec 22, 2010 at 7:21pm
Dec 22, 2010 at 7:25pm
ahann....thats well
Dec 23, 2010 at 2:45am
Important read:
http://www.cplusplus.com/forum/beginner/14081/#msg68655

[edit] FORGET THE REST OF THE CRUFT IN THIS THREAD



People here are just messing around now. Use the method I give in the link if you want to convert a string to upper case. (Presuming locale-sensitive applications aside.)

filipe's std::for_each() throws away the result (and s remains unchanged)

joeneldeasis's strupr() is non-standard, and not C++

rocketboy9000 and PiMaster's loopy code is, well, not (NOT BY LOOP). (Also, it doesn't work right for things like 'á', where std::toupper() should... presuming your locale is properly set. (BTW, you don't need to test whether or not it is already upper before conversion. The function returns the argument character if no change is necessary.)

ne555 knows what he is doing, but a regular expression is totally overkill here -- in any case sed != C++, and he is just messing with you.



The link I gave you gives you working, standard code that will do the job, jumping all the right hoops to work on all modern C++ compilers. Don't let people side-track you.
[/edit]
Last edited on Dec 23, 2010 at 12:06pm
Dec 23, 2010 at 3:59am
you know to best way is? in C.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
main()
{
char *upr;
clrscr();
printf("Enter the word to convert in uppercase: ");
scanf("%s", upr);
strupr(upr);  /*strupr(); converts it in uppercase and strlwr(); converts it in lower case*/
printf("%s",upr);
getch();
}

by joenel :D.. i hope you understand this thing :D
Last edited on Dec 23, 2010 at 4:00am
Dec 23, 2010 at 6:22am
^
Typeless main?
clrscr()?
scanf without bounds checking on an uninitialized pointer??

Don't even bother with that code...
Dec 23, 2010 at 6:31am
1
2
3
4
5
6
huh?
are you a master programmer?
create your own
don't get into this topic
try that code it's working.
duh? :P
Last edited on Dec 23, 2010 at 6:44am
Dec 23, 2010 at 7:31am
No, it doesn't. You need to include conio.h to make it work, which on many systems either doesn't exist or is different. And yes, many of us consider ourselves to be pretty darn good at programming, which is why we help people with their problems. You are not helping. Here is the proper way to solve this problem:
1
2
3
for(int i=0;i<s.length;i++)
	if(s[i]>='a'&&s[i]<='a')
		s[i]+='A'-'a';

or if you want to use toupper:
1
2
3
for(int i=0;i<s.length;i++)
	if(isupper(s[i]))
		s[i]=toupper(s[i]);
Last edited on Dec 23, 2010 at 7:35am
Dec 23, 2010 at 7:47am
@rocketboy9000:
_ First code only changes 'a' to 'A'
_ Second code does nothing

Regex: "y/a-z/A-Z/" (from the rename.pl manual)
Last edited on Dec 23, 2010 at 7:56am
Dec 23, 2010 at 8:10am
My turn! :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
unsigned strlen(const char* str){
	unsigned x=0;
	for(;str[x]!=0;++x){}
	return x;}

unsigned upper(string& str){
	unsigned size=str.length(),x=0;
	for(;x<size;++x){
		if(str[x]>='a' && str[x]<='z'){
			str[x]+='A'-'a';}}
	return x;}

unsigned upper(char* str){
	unsigned size=strlen(str),x=0;
	for(;x<size;++x){
		if(str[x]>='a' && str[x]<='z'){
			str[x]-='a'-'A';}}
	return x;}

unsigned lower(string& str){
	unsigned size=str.length(),x=0;
	for(;x<size;++x){
		if(str[x]>='A' && str[x]<='Z'){
			str[x]+='a'-'A';}}
	return x;}

unsigned lower(char* str){
	unsigned size=strlen(str),x=0;
	for(;x<size;++x){
		if(str[x]>='A' && str[x]<='Z'){
			str[x]+='a'-'A';}}
	return x;}
Dec 23, 2010 at 8:31am
what's wrong with my code? it's easy to understand because the value of upr will be automatically converted to upper case.
i've try this in turbo c 2.01 and but it works fine in c. try it
Last edited on Dec 23, 2010 at 8:35am
Dec 23, 2010 at 8:56am
Oh ****! Serves me right for posting code without testing it.
1
2
3
4
5
6
7
for(int i=0;i<s.length;i++)
	if(s[i]>='a'&&s[i]<='A')
		s[i]+='A'-'a';

for(int i=0;i<s.length;i++)
	if(islower(s[i]))
		s[i]=toupper(s[i]);

These definitely work I have tested them.
Joe, I have tried to compile it, it will only ever work on your horrible computer:
oren-laptop% gcc fail.c
/tmp/ccaxfMBx.o: In function `main':
fail.c:(.text+0xa): undefined reference to `clrscr'
fail.c:(.text+0x38): undefined reference to `strupr'
fail.c:(.text+0x52): undefined reference to `getch'
collect2: ld returned 1 exit status
Dec 23, 2010 at 9:41am
1
2
3
4
5
6
7
8
9
void toupper( char *beg, char *end){
	int dist = end-beg;
	if( dist == 1)
		*beg = toupper( *beg );
	else{
		toupper(beg, beg+dist/2); // execute in parallel
		toupper(beg+dist/2, end);
	}
}


@joeneldeasis: http://www.cplusplus.com/forum/general/33439/#msg179871
Dec 23, 2010 at 12:06pm
Pages: 12