Write a program to find the first and the last occurence of the letter 'o' and character ',' in "Hello, World".
For the last occurence, iterate from end.
2.
Write a program to print a string entered by user.
#include<iostream>#include<string>usingnamespacestd;intmain(){stringx;cout<<"Enter the string"<<endl;cin>>x;cout<<x<<endl;return0;}
3.
Write a program to print every character of a string entered by user in a new line using loop.
#include<iostream>#include<string>usingnamespacestd;intmain(){stringx;cout<<"Enter the string"<<endl;cin>>x;for(inti=0;i<x.length();i++)cout<<x[i]<<endl;return0;}
4.
Write a program to input and display the sentence I love candies.
5.
Write a program to find the length of the string "refrigerator".
6.
Create an array of characters and then print the address of each of the elements of the array. Take difference of two consecutive addresses and compare this with array of integers.
7.
Write a program to enter a string s1 and copy it to another string s2.
You can directly copy std::string with =. You can use strcpy for C-strings or you can make your own function using for loop and copy each character.
8.
Write a program to compare if the two strings entered by user are equal or not without using predefined String functions.
#include<iostream>#include<string>usingnamespacestd;intmain(){stringx,y;cout<<"Enter first string"<<endl;cin>>x;cout<<"Enter secont string"<<endl;cin>>y;boolequal=true;//length must be equalif(x.length()!=y.length()){equal=false;}else{for(inti=0;i<x.length();i++){if(x[i]!=y[i]){equal=0;break;}}}cout<<equal<<endl;return0;}
9.
Write a program to check if the letter 'e' is present in the word 'Umbrella'.
10.
Write a program to check if the word 'orange' is present in the "This is orange juice".
#include<iostream>#include<string>usingnamespacestd;intmain(){strings1="orange";strings2="This is orange juice";strings3;boolpresent=false;for(inti=0;i<(s2.length()-s1.length())+1;i++){s3=s2.substr(i,s1.length());if(s1.compare(s3)==0){present=true;break;}}cout<<present<<endl;return0;}
11.
Write the string after the first occurrence of ',' and the string after the last occurrence of ',' in the string "Hello, Good, Morning".
Write a program that takes your full name as input and displays the abbreviations of the first and middle names except the last name which is displayed as it is. For example, if your name is Robert Brett Roser, then the output should be R.B.Roser.
1. Start from the end.
2. Check for the last word. There will be a space before the first character of the last word or the first character will also be the first character of the string if the string contains only one word.
3. Copy this last word in a new string.
4. Repeat step 2 for the middle word but this time only insert the first character and a dot(.) instead of the whole word in the new string.
5. Repeate till the loop ends by encountering the first character of the string.
13.
Write a program to find the number of vowels, consonents, digits and white space characters in a string.
14.
Write a program to delete all consonents from the string "Hello, have a good day".
15.
Input a string of alphabets. Find out the number of occurrence of all alphabets in that string. Find out the alphabet with maximum occurrence.
Level 2
1.
Write a program to reverse a string with and without using any predefined function.
Write down the names of 10 of your friends in an array and then sort those in alphabetically ascending order.
You can use any sorting algorithm (like one in the practice section of the chapter array) and compare using strcmp function.
9.
Write a program to delete all the consonents from the string "Hello, have a good day".
10.
Write a program to delete the word "the" in the sentence "This is the lion in the cage".
11.
Write a program to check if the two strings entered by user are anagrams or not. Two words are said to be anagrams if the letters of one word can be rearranged to form the other word. For example, jaxa and ajax are anagrams of each other.
A better approach would be to map every character of english alphabets with elements of an array having 26 (or 26*2 considering cases) elements representing each alphabet. Try it yourself.
#include<iostream>#include<string>usingnamespacestd;intmain(){strings1="ajax";strings2="jaxa";boolanagram=true;//only if both strings have same lengthif(s1.length()!=s2.length())anagram=false;else{//matching every element of s2 with every element of s1for(inti=0;i<s1.length();i++){boolfound=false;for(intj=0;j<s2.length();j++){if(s1[i]==s2[j]){//if equal then equating it to empty char//so doesn't match agains2[j]=' ';found=true;break;}}//if not found then it is not an anagramif(!found){anagram=false;break;}}}cout<<anagram<<endl;return0;}
12.
Input a string which contains some palindrome substrings. Find out the position of palindrome substrings if exist and replace it by *. (For example if input string is “bob has a radar plane” then it should convert in “*** has a ***** plane”.
13.
Write a program to replace a given substring in a sentence with another string. For example, in the sentence, ” A batman with bat” if we replace ”bat” with ”snow”, the new sentence should be printed as ”A snowman with snow”.
14.
Write a program to reverse individual words in a string, where each word may be delimited by a dot, comma, space or tab, like www.google.com should become www.elgoog.moc.