-2

I have written my code properly its working fine but i want to convert the both string's first letter into upper case in concatenation i dont know how to do that. I have tried my best to do but i couldn't find any help .help me

void convertToUppercase (char *);
main()
{
char firstString[50];
char secString[50];
char uppercaseFirstString[50];
char uppercasesecString[50];
char concatString[100];
cout<<"Enter String 1 : ";
cin>>firstString;
cout<<"Enter String 2 : ";
cin>>secString;
 cout<<"\n\n";
cout<<"The Length Of String  "<<firstString    <<" is : "<<strlen(firstString);
cout<<"\n";
cout<<"The Length Of String   "<<secString  <<" is : "<<strlen(secString);
strcpy(uppercaseFirstString,firstString );
 strcpy(uppercasesecString,secString);

convertToUppercase(uppercaseFirstString);
 convertToUppercase(uppercasesecString);

cout<<"\n\n";
cout<<"String 1 in upper case : "<<" "<<uppercaseFirstString<<"\n";
cout<<"String 2 in upper case : "<<" "<<uppercasesecString<<"\n";
cout<<"\n\n";
if(strcmp(uppercaseFirstString,uppercasesecString)==0)
{
cout<<"Both Strings are the Same \n";
}
else
{
cout<<"Both Strings are Different \n";
}

strcpy(concatString,firstString);
strcat(concatString,secString);
cout<<"\n\n";
cout<<"Both Strings after Concatenation : " <<concatString; 
return 0;
}
void convertToUppercase (char *sptr)
{
while ( *sptr > '\0' )
{
if (islower(*sptr) )
*sptr = toupper ( *sptr );
++ sptr;
}
}
  • 1
    see this page : http://stackoverflow.com/questions/735204/convert-a-string-in-c-to-upper-case – PRIME Dec 11 '15 at 14:18
  • http://stackoverflow.com/questions/735204/convert-a-string-in-c-to-upper-case – A J Dec 11 '15 at 14:25

2 Answers2

0

Make the following two changes:

1) In the convertToUppercase method, as far as I understood you're trying to convert the first letter of the string to upper case. In that case you don't need a while loop.

Replace the while statement with an if :

if ( *sptr > '\0' )

2) When you're trying to concatenate you're not using the upper case converted string (rather you're using the same input from the user - firstString and secondString).

Replace that with the the upper case string like this :

strcpy(concatString,uppercaseFirstString);
strcat(concatString,uppercasesecString);
RachelD
  • 4,072
  • 9
  • 40
  • 68
Sbhavya
  • 1
  • 2
0

A straightforward approach can look the following way

#include <iostream>
#include <cstring>
#include <cctype>

int main()
{
    char firstString[50] = "aliya";
    char secString[50] = "zafar";
    char concatString[100];

    std::strcpy( concatString, firstString );
    concatString[0] = std::toupper( ( unsigned char )concatString[0] );

    size_t n = std::strlen( concatString );

    std::strcat( concatString, secString );
    concatString[n] = std::toupper( ( unsigned char )concatString[n] );

    std::cout << "\"" << concatString << "\"" << std::endl;                                    
}

The program output is

"AliyaZafar"
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335