Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Indicating the last three characters in a word.

dededede Member Posts: 145
Hello guys.
How can I indicate the last three characters in a string?.
I used find() like in here:
[code]
string inf;
cin>>inf;
inf.find("are");
inf.replace( (inf.size())-3,(inf.length())-3,"o");
cout<<inf<<endl;
[/code]
But then a problem poped up. what if "are" are found twice? like in 'arekare'?, I want to indicate just the last three characters using the standard C++ lib.
Thanks

dede:-)

Comments

  • DonotaloDonotalo Member Posts: 715
    [b][red]This message was edited by Donotalo at 2005-8-5 3:56:35[/red][/b][hr]
    : Hello guys.
    : How can I indicate the last three characters in a string?.
    : I used find() like in here:
    : [code]
    : string inf;
    : cin>>inf;
    : inf.find("are");
    : inf.replace( (inf.size())-3,(inf.length())-3,"o");
    : cout<<inf<<endl;
    : [/code]
    : But then a problem poped up. what if "are" are found twice? like in 'arekare'?, I want to indicate just the last three characters using the standard C++ lib.
    : Thanks
    :
    : dede:-)

    ur program does nothing u want. the [blue]find[/blue] function will return the index where the string found first, nothing else. and the first two parameters of [blue]replace[/blue] function will point to the same index, not last three characters. u may use this:

    replace the last three characters with [blue]x[/blue]:
    [code]
    inf.replace(inf.end()-3, inf.end(), "x");
    [/code]

    replace the last three characters with [blue]abcd[/blue]:
    [code]
    inf.replace(inf.end()-3, inf.end(), "abcd");
    [/code]

    replace the last three characters with [blue]ooo[/blue]:
    [code]
    inf.replace(inf.length()-3, 3, 3, 'o');
    [/code]
  • pseudocoderpseudocoder Member Posts: 701 ✭✭✭
    : [b][red]This message was edited by Donotalo at 2005-8-5 3:56:35[/red][/b][hr]
    : : Hello guys.
    : : How can I indicate the last three characters in a string?.
    : : I used find() like in here:
    : : [code]
    : : string inf;
    : : cin>>inf;
    : : inf.find("are");
    : : inf.replace( (inf.size())-3,(inf.length())-3,"o");
    : : cout<<inf<<endl;
    : : [/code]
    : : But then a problem poped up. what if "are" are found twice? like in 'arekare'?, I want to indicate just the last three characters using the standard C++ lib.
    : : Thanks
    : :
    : : dede:-)

    [blue]You can't guess that the string exists either.[/blue]
    [code]
    if((s.substr(s.length() - 3)) == "are")
    s.replace(s.length() - 3, 3, "xxx");
    [/code]



    :
    : ur program does nothing u want. the [blue]find[/blue] function will return the index where the string found first, nothing else. and the first two parameters of [blue]replace[/blue] function will point to the same index, not last three characters. u may use this:
    :
    : replace the last three characters with [blue]x[/blue]:
    : [code]
    : inf.replace(inf.end()-3, inf.end(), "x");
    : [/code]
    :
    : replace the last three characters with [blue]abcd[/blue]:
    : [code]
    : inf.replace(inf.end()-3, inf.end(), "abcd");
    : [/code]
    :
    : replace the last three characters with [blue]ooo[/blue]:
    : [code]
    : inf.replace(inf.length()-3, 3, 3, 'o');
    : [/code]
    :
Sign In or Register to comment.