How to extract only words with first-letter lowercase from a string in C++

5 Answers

0 votes
#include <iostream>
#include <sstream>
#include <cstring>
#include <iterator>
#include <vector>
 
bool IsLower(char ch);
void GetLowercaseFirstLetterWords(char *s, char *clw);
 
int main()
{
    char s[] = "C++ C c# Java php Go rust";
    char words[256] = "";
 
    GetLowercaseFirstLetterWords(s, words);
 
    std::cout << words;
}

void GetLowercaseFirstLetterWords(char *s, char *words) {
    std::vector<std::string> tokens;
 
    std::istringstream iss(s);
    copy(std::istream_iterator<std::string>(iss),
         std::istream_iterator<std::string>(),
         back_inserter(tokens));
         
    for (auto word : tokens) {
        if (IsLower(word[0])) {
            strcat(strcat(words, word.c_str()), " ");
        }
    }
}

bool IsLower(char ch) {
    if (ch >= 'a' && ch <= 'z') {
        return true;
    }
 
    return false;
}
 
 
/*
run:
 
c# php rust 
 
*/

 



answered Feb 4, 2017 by avibootz
edited Apr 13, 2024 by avibootz
0 votes
#include <iostream>
#include <sstream>
#include <cstring>
#include <iterator>
#include <vector>
 
bool IsLower(char ch);
void GetLowercaseFirstLetterWords(char *s, char *clw);
 
int main()
{
    char s[] = "C++ C c# Java php Go rust";
    char words[256] = "";
 
    GetLowercaseFirstLetterWords(s, words);
 
    std::cout << words;
}

void GetLowercaseFirstLetterWords(char *s, char *words) {
    char *p;
 
    p = strtok(s, " ");
    while (p != NULL) {
        if (IsLower(p[0])) {
            strcat(strcat(words, p), " ");
        }
 
        p = strtok(NULL, " ");
    }
}

bool IsLower(char ch) {
    if (ch >= 'a' && ch <= 'z') {
        return true;
    }
 
    return false;
}
 
 
 
/*
run:
 
c# php rust 
 
*/

 



answered Feb 4, 2017 by avibootz
edited Apr 13, 2024 by avibootz
0 votes
#include <iostream>
#include <string>
#include <vector>
  
std::vector<std::string> extract_only_words_with_first_letter_lowercase(std::string s) {
    std::vector<std::string> words;
 
    size_t start = 0, end;
    while ((end = s.find(" ", start)) != std::string::npos) {
        std::string word = s.substr(start, end - start);
        if (std::islower(word[0])) {
            words.push_back(word);
        }
        start = end + 1;
    }
      
    if (std::islower(s.substr(start)[0])) {
        words.push_back(s.substr(start));
    }
      
    return words;
}
  
int main() {
    std::string s = "C++ is a High-level General-purpose pRogramming language";
      
    std::vector<std::string> words = extract_only_words_with_first_letter_lowercase(s);
  
    for (const std::string& w : words) {
        std::cout << w << std::endl;
    }
}
  
  
  
   
/*
run:
   
is
a
pPogramming
language
   
*/

 



answered Apr 13, 2024 by avibootz
0 votes
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::vector<std::string> extract_only_words_with_first_letter_lowercase(std::string s) {
    std::istringstream iss(s);
    std::string word;
    std::vector<std::string> words;

    while (iss >> word) {
        if (!word.empty() && std::islower(word[0])) {
            words.push_back(word);
        }
    }
  
    return words;
}
   
int main() {
    std::string s = "PHP rust JavaScript c C++ java Typescript python";
       
    std::vector<std::string> words = extract_only_words_with_first_letter_lowercase(s);
   
    for (const auto& w : words) {
        std::cout << w << " ";
    }
}


    
/*
run:
    
rust c java python 
    
*/

 



answered Apr 13, 2024 by avibootz
edited Jan 16 by avibootz
0 votes
#include <iostream>
#include <regex>
#include <string>
#include <vector>

// Function to extract words starting with lowercase
std::vector<std::string> extractLowercaseWords(const std::string& text) {
    std::regex pattern(R"(\b[a-z]\w*)");
    std::sregex_iterator it(text.begin(), text.end(), pattern);
    std::sregex_iterator end;

    std::vector<std::string> result;

    while (it != end) {
        result.push_back(it->str());
        ++it;
    }

    return result;
}

int main() {
    std::string s = "PHP rust JavaScript c C++ java Typescript python";

    std::vector<std::string> result = extractLowercaseWords(s);

    for (const auto& w : result) {
        std::cout << w << " ";
    }
}



/*
run:

rust c java python 

*/

 



answered Jan 16 by avibootz

Related questions

...