Function does not return string

Hello/Aslaam all,

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.
Last edited on
You're only incrementing your pointer if the character islower.
Move the ++sptr; out of the if-statement.

Also, main's signature should return int.

1
2
3
4
int main()
{
     // ...
}
Last edited on
Thank you tpb and gandadu...okay i got it tpb..
Topic archived. No new replies allowed.