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

4 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 <string>
#include <vector>
#include <sstream>
  
std::vector<std::string> extract_only_words_with_first_letter_lowercase(std::string s) {
    std::vector<std::string> words;
    std::istringstream ss(s);
   
    std::string word; 
   
    while (ss >> word)  {
       if (std::islower(word[0])) {
            words.push_back(word);
        }
    }
 
    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
...