can someone help me regarding my program? i am tasked to create a program called string censoring. it asks for two strings then it removes the characters in the first string which are similar to those in the second.
Ex:
1st string= new york yankees
2nd string= absolutely
result= nw rk nk

and another thing, it has to be case sensitive..haaayy...

here's the code that i've done but it doesn't work correctly..can someone help me out?

#include <iostream>

#include <string>

#include <conio.h>

using namespace std;



int main(){

    

    cout << "Input string" << "\n\n" << endl;

    string firstStr;

    cin >> firstStr;

    cout << "Input second string." << "\n\n" << endl;

    string secondStr;

    cin >> secondStr;                                  

    string newStr;

    int j = secondStr.length();

    for (int k = 0; k != j; k++){

          int i = firstStr.find(secondStr[k]);

          int gPos = i;

          if (gPos != string::npos){

                   while (gPos != string::npos){

                         newStr = firstStr.substr(0,i);

                         newStr += firstStr.substr (i + j);

                   }

          }else break;

    }    

    cout <<"''"<< firstStr <<"''"<< " with ''" << secondStr << "'' removed becomes ''"

    << newStr << "''" << endl;

    getch();

}

help me out please..

i do not understand what have you done here...

if (gPos != string::npos){

                   while (gPos != string::npos){

                         newStr = firstStr.substr(0,i);

                         newStr += firstStr.substr (i + j);

                   }

what is npos??? on my compiler it is giving a very larg about 10 digit number...and your while loop is indefinite...

here's the code that i've done but it doesn't work correctly..can someone help me out?

First off, please be a little clearer. Explain, don't just say it doesn't work. Why doesn't it work? What does it do?

What I'd do is immediately after the FOR, start the WHILE (or preferably DO/WHILE). Use the WHILE to find each letter and remove it, look up STRING.ERASE(). Exit the WHILE when the current letter is not found.

i have used shifting the characters to left if a matched character in s1 and s2 (two strings) is found...

example..
s1="new york yankees"
s2="absolutely"

when a is found in s1 at position 10 then i shifted nkees to left and deducted length of s1 by 1...
similarly doing this for each character of s2 we will get the result...

but i wonder my solution is not efficient one...can i post my code

i have used shifting the characters to left if a matched character in s1 and s2 (two strings) is found...

If you're using C++ strings, why? Just use .erase() If you're using C-strings, that's the only way to do it.

can i post my code

How else can we look at it? Your problem seems to be the same as the OP's so I guess it's not a case of thread hijacking.

If you're using C++ strings, why? Just use .erase()

yes i am using c++ strings...but i tried that function.. it doesn't erase one character but it erase the following part of the substring...
for eg .
a="abcdefgh"
if i use a.erase(4) it doesn't erases 'e' but it erases f ,g and h also...

may be i am not using it in a proper way...kindly explain..

How else can we look at it? Your problem seems to be the same as the OP's so I guess it's not a case of thread hijacking.

http://www.daniweb.com/forums/post502988.html#post502988
after this i am afraid of posting anycode here....

i just wrote if((year%100==0 && year%400==0) || (year%100!=0 && year%4==0)) in post number 3...
for which i got a bad post flag saying i did javaAddict's homework...

so with post number 5 i removed that condition in if clause and wrote that thing(which u can see)...
so i got another badpost flag saying IT WASN'T ALSO A GOOD SUGGESTION.....

that's why i asked for posting my code..

A simple Google search came up with this in seconds. You might want to try it yourself next time. It's faster.

i didn't google for it but i read it in the library files of my dev-cpp compiler...i have seen those three methods..but i am not very much familiar with the concept of iterators....

what do i need to do extra after calling erase function??

here is my code using shifting of string....

#include <iostream>
#include <cstring>
using namespace std;
void shift(string &s,int len,int index)
{
     if(index>len)
     return;
     
     int i;
     for(i=index ; i<len-1 ; i++)
     s[i]=s[i+1];
     s.erase(i);
     
     
}
int main()
{
        string a,b;
        
        cout<<"\nEnter First String : ";
        getline(cin,a);
        cout<<"\nEnter Second String : ";
        getline(cin,b);
        
        cout<<"\nStrings entered "<<a<<" and "<<b<<endl;
        
        string::const_iterator it1=b.begin();
        
      

        while(it1!=b.end())
        {
                           
                  int p,len;
                  while((p=a.find(*it1))<(len=a.length()) && p>0)                                    
                  shift(a,len,p);
                  
                  it1++;
        }

        cout<<"\nFirst String after subtracting second from this : "<<a;                                                                                       
                  
        cin.get();
        return 0;
}

it's working fine but i know it's not the best way to do it..so what's the best way??(in terms of efficiency and space used..and a condensed code)

The erase() method of the STL class is overloaded to accept several different parameters. One version allows you to delete a single char at a specific address using an iterator to specify the address. One version allows you to erase a range of characters from the string using two iterators, one to specify the address at where to start and one to specify the address at where to stop. The third allows you to specify where to start using an index---that defaults to zero---- and how many characters to erase---that defaults to npos. Therefore, when you write a.erase(4) and it erases efgh from abcdefgh I suspect it's because it starts erasing from index 4, in this case a[4] == 'e', and erases until the end of the string, since only a single int was passed to erase(). If you had written a.erase(4, 2) it would have removed ef and left abcdgh.

If you want to delete an entire substring from a given string, then you'll want to find the first index position of the substring using the appropriate version of the find() method of the STL string class and pass that and the length of the substring to the erase method using the version of erase() that takes to ints (or size_types).

There's a pretty good presentation of STL container methods at cppreference.com.

RajatC
Please take the time to look up information if you don't recognize something, rather than hijack someone's thread. string::npos is a STL object used to mean any index at or past the end of a string.

anallan
You actually have the right idea, but you made three (small) mistakes. The code in question is here:

for (int k = 0; k != j; k++){

          int i = firstStr.find(secondStr[k]);

          int gPos = i;

          if (gPos != string::npos){

                   while (gPos != string::npos){

                         newStr = firstStr.substr(0,i);

                         newStr += firstStr.substr (i + j);

                   }

          }else break;

    }

Your correct idea is to first find the index of the current second string character (you do this on line 3) and if the character is found in the first string (line 7) you want to erase it.

Your second correct idea is that you don't want to move on to the next character in the second string until you have eradicated all occurrences of the current second string character out of the first string.

Your first mistake is the position of the loop (line 9). Currently, gpos will never change. Every time you loop you need to update gpos just like you did on lines 3 and 5. (It will be easier if you get rid of the variable i and just use gpos.)

The second mistake is how you attempt to get rid of the found character in the first string (lines 11 and 13). WaltP correctly suggested that you use the string::erase() method: firstStr.erase( gpos, 1 ); The third mistake is on line 17. You'll have to think about it to see why it is a mistake.

Hope this helps.

i've ended up with my codes below which actually works..thanks for all of your help.
aside from my use of getch for so many times(i just love to use em coz of their effects^^),how can i make this more efficient(i.e.shorter,etc.)?

#include <iostream>

#include <string>

#include <conio.h>

#include <cctype>



using namespace std;



int main(){

    

    cout << "Input string" << "\n\n" << endl;

    string firstStr;

    getline(cin,firstStr);

    cout<<"\n"<<endl;

    getch();

    cout << "Input second string." << "\n\n" << endl;

    string secondStr;

    getline(cin,secondStr);

    cout<<"\n"<<endl;

    getch();

    string copyStr = firstStr;                                  

    int len = secondStr.length();

    string newStr;

 //get the two strings, create a copy for the first string

 //and get the length of the second string

    

    for (int k = 0;k != len;k++){

        while(1){

       int gPos = firstStr.find(secondStr[k]);

        if(gPos != string::npos){

                newStr= firstStr.substr(0,gPos);

                newStr += firstStr.substr (gPos + 1);

                firstStr = newStr;}else break;}

    }

    

 //while a character in the second string exists in the first,
 //characters in the second will be extracted from the first string.

                                

    cout <<"''"<< copyStr <<"''"<< " with ''" << secondStr << "'' removed becomes ''"

    << newStr << "''" << endl;

    getch();

    getch();

}

It looks good as is to me.

However, you really should get rid of that <conio.h> stuff. It is not a good idea to mix C++ and C I/O. If you really must have a getch() type thing in there, use one of the C++ methods to do it.

best: cin.ignore( numeric_limits<streamsize>::max() ); also good: getline( cin, foostr ); cin.get( foochar ); Hope this helps.

Also stop double-spacing your code.

And you do not need getch(). Learn to program without it.

cin.ignore( numeric_limits<streamsize>::max() );

can you explain this to me? what does this line mean?

Edited 3 Years Ago by mike_2000_17: Fixed formatting

When in doubt, read the documentation.

I just did, and realized I made an error. It should be: cin.ignore( numeric_limits<streamsize>::max(), [B]'\n'[/B] ); Hope this helps.

This question has already been answered. Start a new discussion instead.