String Question 1

Question Asked in Companies like Sap labs , payu  etc  


Q.) You are given a string 'STR'. You have to convert the first alphabet of each word in a string to UPPER CASE.

For example:

If the given string 'STR' = ”I am a student of the third year” so you have to transform this string to ”I Am A Student Of The Third Year"

Note:

'STR' will contains only space and alphabets both uppercase and lowercase. The words will be separated by space.

Complete the function :
#include <bits/stdc++.h>
convertString(string str){
}

Here is the solution and Explanation :-
#include <bits/stdc++.h>
convertString(string str){
for(int i =0; i<str.length(); i++){
if( i ==0; || str [i-1] ==''){
str[i] = toupper(str[i]);
}
}
return str;
}

EXPLANATION :

#include<bits/stdc++.h> 
1) This includes all the standards library 
using namespace std;
string converString(string str)
2) This function takes a string single argument a string str and will return the string 
for(int i =0; str.length(); i++)
3)This for loop run and iterates over each character int he string the index 'i'.
if(i == 0 || str[i-1] == ' ')
4)  i == 0 check if current characters is  first character of the string 
    str[i-1]== ' '  this checks if previous character is a space , indicating the start of a new word 
    if condition is true it means current characrer is the first letter of a word 

5) str[i] = toupper(str[i]) this will convert the character to upper string 
6) return str; this will return the string


Code by Rehyan yadav 
for more updates follow our page @alphacodes or go to our webiste just search 
alphacodes101 and ues our services and explore our page for more 








Comments

Popular Posts