uppercase array

I'm doing a function which get an pointer to char and return a pointer to char...it's supposed to get lower case letters and return upper case letters, I have done this at this moment...If I did'nt have to do it through arrays and pointer I can do it easily just adding the difference between lowcase and upper case...but something is going bad through my pointer...

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
#include <stdio.h>
#include <iostream>
using namespace std;

char* uppercase(char *array){
	
	while(*array != '\0'){
		*array -= 32;
		array++;
	}
	return array;
}

int main(){ 
	
	char letters[] = {'a','b','c','d','e','f','g','h','\0'};
	for(auto x:letters){
		cout<<x<<" ";
	}
	char* ptr;
	ptr = uppercase(letters);
	while(*ptr != '\0'){
		cout<<*ptr<<" ";
		ptr++;
	}
	return 0;
}
Last edited on
Change this:
1
2
3
4
5
6
7
8
char* uppercase(char *array){
	char*result = array;
	while(*array != '\0'){
		*array -= 32;
		array++;
	}
	return result;
}
Otherwise uppercase(...) will return the end of the string (actuall the pointer to '\0')
Thanks, that's true...now I'm doing a rotation13...I mean if I get a letter I must send the same letters plus 13 position more in the alphabet...but of course if it get further than Z it must start from A...I have done this at the moment..I'm getting strange simbols...I think it's because the if sentece is altering the result..
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
34
35
36
37


#include <iostream>
#include <stdio.h>

using namespace std;

char* rot13(char* array){
	char* copy = array;
	while(*array != '\0'){
		if((*array + 13)<='Z'){
		*array += 13;
		array++;
		}
		else{
			*array -= 11;
			array++;
		}
	}
	return copy;
}

int main(){

char letters[] = {'H','E','L','L','O','\0'};

char *rot13ptr = rot13(letters);

while(*rot13ptr != '\0'){
		cout<<*rot13ptr;
		++rot13ptr;
}

return 0;
}

It works for me. Except that line 16 should be: *array -= 13;

You can move array++; out of the if/else so that it appears only once.

Further more line 25 can be written as:

char letters[] = "HELLO";

What about non ASCII letters like space?
I have optimized those things....and I have change the input in char array and I have written a sentence with spaces, on the place where a space is, it is giving me this symbol _ it matches with 45 number in ASCII code and space is 32 so it's working fine 13 + 32 = 45....but I see what you mean....the target would be skip white space...I guess I could fix that including other if statement...but maybe there are functions to get an string and skip the white space but I don't know the result( i guess without doing any more change I could get the sentence rotate 13 position in ASCII code but without space).....I have done this if you can provide a better version I would really appreciate since I haven't got use to work with string...
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
34
35
36
37
38
39
40
41
42


#include <stdio.h>
#include <iostream>
using namespace std;

char* uppercase(char *array){
		char *copy = array;
		while(*array != '\0'){
			if(*array=='  '){
				
				
			}
			else if((*array + 13)<='Z'){
				*array +=13;
				
			}
			else{
				*array -=13;
				
			}
			array++;
			
		}
	return copy;
}

int main(){ 
	
	char letters[] = {"HELLO MY FRIEND"};
	for(auto x:letters){
		cout<<x<<" ";
	}
	cout<<endl;
	char* ptr;
	ptr = uppercase(letters);
	while(*ptr != '\0'){
		cout<<*ptr<<" ";
		ptr++;
	}
	return 0;
}
You can use isalpha(...) and toupper(...):

http://www.cplusplus.com/reference/cctype/isalpha/?kw=isalpha
http://www.cplusplus.com/reference/cctype/toupper/?kw=toupper

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char* uppercaserot13(char *array){
		char *copy = array;
		while(*array != '\0'){
			if(isalpha(*array)){ // Note: Only alpha characters are shifted
				
				
			if(toupper(*array + 13)<='Z'){ // Note: Now, lower case characters are shifted as well
				*array +=13;
				
			}
			else{
				*array -=13;
				
			}
			}
			array++;
			
		}
	return copy;
}
I have done those things and they work almost all right...I have done two sections to analysis...one with lowercase letters and one with uppercase letters.. with uppercase it's working fine but with lowercase I'm getting an odd...with letter y I'm getting L while with letter Y I'm getting F...I think is odd since both string use the same rotate13 function.... and I would like to use the same char array than create another one...I have tried to use it again but I just get errors...I guess I will have to place the information through a pointer but I'm not sure...

And I have used strcpy to changed to cointained of the string but I would like to do that without including cstrig....is that possible?? I guess with pointers but I'm not sure...

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;

char* rotate13(char *array){
		char *copy = array;
		while(*array != '\0'){
			if(isalpha(*array)){
			if(toupper(*array + 13)<='Z'){
			  *array = toupper(*array +=13);
				
			}
			else{
				*array = toupper(*array -=13);
				
			}
			}
			array++;
			
		}
	return copy;
}

int main(){ 
	
	char letters[] = {"HELLO MY FRIEND"};
	for(auto x:letters){
		cout<<x<<" ";
	}
	cout<<endl;
	char* ptr;
	ptr = rotate13(letters);
	while(*ptr != '\0'){
		cout<<*ptr<<" ";
		ptr++;
	}
	cout<<endl;
	
	strcpy(letters,"hello my friend");// I have included <cstring> to do this operation
	for(auto x:letters){				//is that possible to do it without inlcuding <ctring>
		cout<<x<<" ";
	}
	 
	cout<<endl;
	ptr = rotate13(letters);
	while(*ptr != '\0'){
		cout<<*ptr<<" ";
		ptr++;
	}
	return 0;
}

The problem is that a the upper case 'Y' + 13 is a lower case 'f'.

Change your function to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char* rotate13(char *array){
		char *copy = array;
		while(*array != '\0'){
			if(isalpha(*array)){
			*array = toupper(*array); // Note: toupper before the calculation
			if((*array + 13)<='Z'){
			  *array+=13;
				
			}
			else{
				*array-=13;
				
			}
			}
			array++;
			
		}
	return copy;
}


is that possible to do it without inlcuding <ctring>
You can always make another variable like letters2 or so.
Last edited on
I was thinking that the trouble was on the lowercase, I mean when it was getting the letter from a lowercase...but that's true..the uppercase was resulting a lowercase (f) and becoming again in uppercase, so it was smaller than 'z'...great!!!!, but that leads me to other questions then...It's regarding to my last code( with your change it doesn't happen) when we plus 13 to y there is an overload in the ASCII code( at least I think it like that), so the result value would be smaller than Z so it would apply y +13 that is...End of transmission in ASCII....obviously it can't do an overload..it must be adding the ASCII code of y plus 13 and it gets an int which is bigger than the int correspondent to 'Z' , but I would like a confirmation because I have been thinking a bit about that...thanks!!
Yes, the operation + 13 implicitly promotes char before the addition to an int. The sign is maintained
Sorry, I don't get what you mean...I think you mean that char is converted to a int a later it's added to 13 so I have a number bigger than the z equivalent...is that right??
Yes.
Topic archived. No new replies allowed.