Home > Net >  Display words that do not contain the specified letter
Display words that do not contain the specified letter

Time:11-22

#include <string>
#include <windows.h>
#include <stdlib.h>
#include <sstream>
using namespace std;
 
int main()
{
    string s = "We study C   programming language first semester.", word;
    cout << s << endl;
    s.pop_back();
    stringstream in (s);

    
    while (in >> word)
    {
        if (word.find('e') == std::string::npos)
        cout << word << endl;            
    }
    
   
system("pause");
return 0;
}


Hello everyone! Given the line "We study C programming language first semester." And it is necessary to deduce words from it that do not contain the letter 'e'. There is such a version of the program, but I need something simpler without stl. Using canned loops, etc and preferably using char I will be very grateful to everyone for their help!

CodePudding user response:

If you really want to go with only std::string and loops, I guess maybe this? But it's a lot longer than just using stringstream and such,

#include <iostream>
#include <string>

int main()
{
    std::string s = "We study C   programming language first semester.";
    s  = ' '; //safeguard;

    std::string word = "";
    for (char c : s)
    {
        if (c == ' ') //break of word
        {
            if (word != "") //word not empty
            {
                bool incE = false;
                for (char c2 : word) //check for e
                {
                    if (c2 == 'e') {incE = true; break;}
                }

                if (!incE)
                {
                    std::cout << word << " ";
                }

                word = "";
            }
        }
        else {word = word   c;}
    }
}

Output: study C programming first

  •  Tags:  
  • c
  • Related