The function that has a string parameter and returns a copy of the parameter which contains only the letters in the original string converted to upper case.
Question
The function that has a string parameter and returns a copy of the parameter which contains only the letters in the original string converted to upper case.
plz, make a function that has a string parameter and returns a copy of the parameter with arbitrary upper case letters inserted into the copy of the string until its length is a multiple of 4.
A function that has a string parameter and that
(1) removes all nonletters from the string and converts all the letters to upper case
(2) adds arbitrary upper case letters to the string until the string’s length is a multiple of 4
(3) moves all the letters at even positions in the string, in order, to the end of the string.
the function returns the edited string.
If the original string is
Meet me tonight
then the function returns
Summary
In this question, we have to write a function that will take a string as a parameter and it will do some given operation on that string and return the new string.
Firstly, in this function, we will be removing all the non-letters(including whitespaces), and then the letters will be converted into upper case.
Then, we will check whether the new string’s length is the multiple of 4 or not, if the length is not the multiple of 4 we will add arbitrary characters at the end to match the size of the string to a multiple of 4, otherwise, we will not do anything.
Then, we will move the letters at the even positions to the end of the string.
At last, we will return the new converted string.
Explanation
We will implement a function “convert_string” and we will pass an input string “str” to that function.
Firstly, a loop will be executed and it will check if each character of the input string is a letter. If it is a letter, then we will convert that letter to uppercase and add it to the new string. If it is not a letter then we will simply ignore that letter.
After converting every valid letter into its uppercase, we will check the size of the new string that if its length is a multiple of 4 or not.
If its length is not a multiple of 4, then we will add some alphabets in uppercase to match the size with a multiple of 4.
Then, we will take a new string and we will run a loop in which the characters at the even positions are added to it first and then the characters at the odd positions. We can understand it clearly with the example below.
Example:
String = “Convert a string”
After step-1: CONVERTASTRING
After step-2: CONVERTASTRINGAB
After step-3: CNETSRNAOVRATIGB
Code
#include <iostream> using namespace std; string convert_string(string str) { string new_str=""; /* to remove non-letters and convert the letters to uppercase */ for(int i=0;i<str.length();i++){ char c=toupper(str[i]); if(str[i]>='a'&& str[i]<='z' || str[i]>='A'&& str[i]<='Z') new_str=new_str+c; } /* to add arbitrary upper case lterrs to string until length becomes multiple of 4 */ int num =4-new_str.length()%4; for(int i=0;i<num;i++) new_str=new_str+char(65+(i%25)); /* to remove all letters at even postions and add at the last */ string ans=""; for(int i=0;i<=str.length();i+=2) ans=ans+new_str[i]; for(int i=1;i<=str.length();i+=2) ans=ans+new_str[i]; return ans; } /* driver code */ int main() { string s="Convert a string"; cout<<"The converted string is: "; cout<<convert_string(s)<<endl; }
Output
Also read, using c++ implement the following method