Strings Challenges | C++ Placement

 String Challenges 

Challenge 1 

UpperCase-LowerCase interconversion 

Given a string s with both uppercase and lowercase latin characters (‘a’ - ‘z’).  Your task is convert whole string into  

1. Lower Case 

2. Upper Case 

 

Base idea: ‘a’ - ‘A’ = 32 

1. Lowercase to UpperCase Approach 

1. Iterate over the string s and if s[i] is a lower case character, then update s[i] -= 32 

 

2. UpperCase to LowerCase Approach 

1. Iterate over the string s and if s[i] is a upper case character, then update s[i] +=32 

 

Code:

  



Challenge 2

Form the biggest number 

of integers, our task is to form the biggest number out of those

numbers in the string. 

 

Approach: 

Sort the string in descending order using inbuilt sort function. 

 

Code 

  


Challenge 3

Max Frequency 

s of latin characters, your task is to output the character which has 

maximum frequency. 

 

Approach: 

Maintain frequency of elements in a separate array and iterate over the array and find the maximum frequency character. 

 

Code 

  


Challenge 4 (Additional Question) 

Compression of strings 

s, your task is to remove the repeating consecutive characters.

 

Approach: Create an answer string and iterate in the string from i=1 and check if it is equal to the previous character. Two cases arise 

1. s[i] = s[i-1] - then do not push_back the it h character to the answer string. 

2. s[i] != s[i-1] - then push_back the i th character to the answer string. 

 

Code 


Comments

Popular posts from this blog

Arrays - Q's asked by Top MNC's

Subarrays - Challenges | Questions asked by Top MNC's |