adding string in a string....


i try to write a program which can add string in a certain string



example word :

i try to input anyword which ends in 'y' can be
output will be: happiest

i refer my work shown below but i cannot do this//is it possible..
any one help me..







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
using namespace std;

int main ()
{
	char a[20];

  string word;
  cout<<"Enter word:  ";
  cin>>word;

  string str;
  string str2=word;
 


  str.app(endstr2);                  
  str.append(str2,2,1);                 
      str.append(1,'sh');                   
 
   
  cout << str << endl;
  return 0;
}
Last edited on
If you are trying to change eg. "happy" to "happiest"

try to see if this code is what you are looking for:
1
2
3
4
5
6
string sample = "Happy";
if ( sample[ sample.length()-1 ] == 'y' ) // if the last character in the string is a 'y'
{
     sample[ sample.length()-1 ] = 'i'; // change that 'y' into an 'i'
     sample += "est"; // add "est" to the string 
}
Topic archived. No new replies allowed.