i want to convert a string into uppercase letters but the function does not return any values( i guess). I am only getting the answer of the first cout statement. kindly help me out where I am mistaken.
//this program converts a string into uppercase
#include<iostream>
#include<stdlib.h>
#include<ctype.h>
using namespace std;
void convertToUppercase(char*);
main()
{
char s[30] = "Welcome to Pakistan";
cout<<" the string before conversion ==> "<<s<<endl;
convertToUppercase(s); //function call
cout<<" the string after conversion ==> "<<s;
}
void convertToUppercase(char *sptr)
{
while(*sptr !='\0')
{
if(islower(*sptr))
{
*sptr = toupper(*sptr); //convert to uppercase character
++sptr;
}
}
The problem is that your increment of sptr is inside the if(islower()) block, so it's only incremented if the current char is lowercase. It needs to be after the block.
1 2 3 4 5 6
char *str_toupper(char *s) {
for ( ; *s; ++s)
if (islower(*s))
*s = toupper(*s);
return s;
}
BTW, remember to post in [code][/code] tags in the future.