mardi 23 juillet 2019

How to convert Alpha Numerical and special string to alphabetical string?

In a function a string is passed which has alphabets, numbers and special characters, the function should return only the alphabets from the string.

I tried following code but it is giving along with the numbers as well. Where am I going wrong?

In this code std::string::find_first_not_of stl algorithm is being used

 std::string jobName1 = "job_2";

 std::size_t found = 
  jobName1.find_first_not_of("abcdefghijklmnopqrstuvwxyzABCDEFGHIJK
  LMNOPQRSTUVWXYZ");
 while (found!=std::string::npos)
 {
    jobName1.erase(found,1);
    found=jobName1.find_first_not_of("abcdefghijklmnopqrstuvwxyzAB
    CDEFGHIJKLMNOPQRSTUVWXYZ",(found+1));
 }

Input:

job_2

Output:

job2

Expected:

job

Aucun commentaire:

Enregistrer un commentaire