Jump to content

Converting an array with elements to uppercase.C

7 minutes ago, FreQUENCY said:

If i loop over the elements and use 


words[i]=touper(words[i]) 

i will get an error !

First of all, toupper() only converts a single character, not a string. Secondly, you're not iterating over the characters.

 

The following will do it for you:

for(int i=0; i<5; i++)
	for(int j=0; j<9; j++)
		words[i][j] = toupper(words[i][j]);

 

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

10 minutes ago, FreQUENCY said:

If i loop over the elements and use 


words[i]=touper(words[i]) 

i will get an error !

toupper works on a single character. You need to write a function that loops over all characters in a string and converts them to uppercase:

 

//Include ctype.h for toupper
#include <ctype.h>

//Function takes a c string pointer and converts it to uppercase
void
strtoupper(char* str)
{
	while (*str)
	{
		*str = toupper(*str);
		++str;
	}
}

//Example use in your case:
strtoupper(words[i]);

 

Link to post
Share on other sites

1 minute ago, WereCatf said:

First of all, toupper() only converts a single character, not a string. Secondly, you're not iterating over the characters.

 

The following will do it for you:


for(int i=0; i<5; i++)
	for(int j=0; j<9; j++)
		words[i][j] = toupper(words[i][j]);

 

I am off to a math class.I will test your code when i arrive. Thank you very much @WereCatf

Link to post
Share on other sites

2 minutes ago, Unimportant said:

while (*str)

That's a little risky. If OP somehow messes up and there's no NULL-character stored in the array, you'll end up overwriting who knows how much memory with that and crashing. That is the cleaner approach for someone who knows to always make sure there is a NULL there, but for the OP, I believe sticking to specific depth, like in my approach, is better while they're still learning.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

19 minutes ago, WereCatf said:

That's a little risky. If OP somehow messes up and there's no NULL-character stored in the array, you'll end up overwriting who knows how much memory with that and crashing. That is the cleaner approach for someone who knows to always make sure there is a NULL there, but for the OP, I believe sticking to specific depth, like in my approach, is better while they're still learning.

Presumably he'll want to print the result after converting it to uppercase.

He'll get the same surprise when printf does not find a terminating 0.

Link to post
Share on other sites

Just now, Unimportant said:

Presumably he'll want to print the result after converting it to uppercase.

He'll get the same surprise when printf does not find a terminating 0.

Yeah, but at least he's not overwriting random memory, only printing it.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Just now, WereCatf said:

Yeah, but at least he's not overwriting random memory, only printing it.

Doesn't really matter. It'll simply crash with a segfault both ways.

Your application can't mess with another application or the OS's memory if that is your fear.

Link to post
Share on other sites

Just now, Unimportant said:

Your application can't mess with another application or the OS's memory if that is your fear.

I know.

Hand, n. A singular instrument worn at the end of the human arm and commonly thrust into somebody’s pocket.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×