#include <iostream>
#include<bits/stdc++.h>
using namespace std;
// Capitalize first and last character of a each word in a given string
string FirstAndLast(string str)
{
// Create an equivalent string
// of the given string
string ch = str;
for (int i = 0; i < ch.length(); i++)
{
// k stores index of first character
// and i is going to store index of last
// character.
int k = i;
while (i < ch.length() && ch[i] != ' ')
i++;
// Check if the character is a small letter
// If yes, then Capitalise
ch[k] = (char)(ch[k] >= 'a' && ch[k] <= 'z'
? ((int)ch[k] - 32)
: (int)ch[k]);
ch[i - 1] = (char)(ch[i - 1] >= 'a' && ch[i - 1] <= 'z'
? ((int)ch[i - 1] - 32)
: (int)ch[i - 1]);
}
return ch;
}
//Add a word in middle of a given string
void insertDemo(string str, string str1)
{
// Inserts str1 in str starting
// from 6th index of str
str.insert(12,str1, 0,11);
cout << "After addition of word : ";
cout << str;
}
int main()
{
char str[200], str1[57], wrd[20];
int i, j, strLen, wrdLen, tmp, chk=0;
cout<<"Enter the String: ";
gets(str);
cout<<"Enter the Word to delete: ";
cin>>wrd;
cout << "Enter new word to be added in the middle : ";
cin>>str1;
cout << "You entered: " << str << endl;
cout <<"\n\nCapitalize first and last character :"<< FirstAndLast(str)<<endl;
cout << "Original String : " << str << endl;
insertDemo(str, str1);
//Delete a word from a given string. Pop-up a message if the string is empty
strLen = strlen(str);
wrdLen = strlen(wrd);
for(i=0; i<strLen; i++)
{
tmp = i;
for(j=0; j<wrdLen; j++)
{
if(str[i]==wrd[j])
i++;
}
chk = i-tmp;
if(chk==wrdLen)
{
i = tmp;
for(j=i; j<(strLen-wrdLen); j++)
str[j] = str[j+wrdLen];
strLen = strLen-wrdLen;
str[j]='\0';
}
}
cout<<"\nNew String after deletion = "<<str<<endl;
return 0;
}
Comments
Leave a comment