Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,884 questions

51,810 answers

573 users

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

3 Answers

0 votes
#include <iostream>
#include <cstring>

bool IsUpper(char ch);
void GetUppercaseFirstLetterWords(char *s, char *clw);
 
int main()
{
    char s[] = "C++ C c# Java php Go";
    char words[64] = "";
 
    GetUppercaseFirstLetterWords(s, words);
 
    std::cout << words;
 
}

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

bool IsUpper(char ch)
{
    if (ch >= 'A' && ch <= 'Z') {
        return true;
    }
 
    return false;
}
 
 
 
/*
run:
 
C++ C Java Go 
 
*/

 



answered Feb 3, 2017 by avibootz
edited Apr 13, 2024 by avibootz
0 votes
#include <iostream>
#include <sstream>
#include <cstring>
#include <iterator>
#include <vector>
 
using std::string;
using std::istream_iterator;
 
bool IsUpper(char ch);
void GetUppercaseFirstLetterWords(char *s, char *clw);
 
int main()
{
    char s[] = "C++ C c# Java php Go";
    char clw[30] = "";
 
    GetUppercaseFirstLetterWords(s, clw);
 
    std::cout << clw;
}

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

bool IsUpper(char ch) {
    if (ch >= 'A' && ch <= 'Z') {
        return true;
    }
 
    return false;
}
 
 
/*
run:
 
C++ C Java Go 
 
*/

 



answered Feb 3, 2017 by avibootz
edited 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_uppercase(std::string s) {
    std::vector<std::string> words;
    std::istringstream ss(s);
   
    std::string word; 
   
    while (ss >> word)  {
       if (std::isupper(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_uppercase(s);
  
    for (const std::string& w : words) {
        std::cout << w << std::endl;
    }
}
 
  
   
/*
run:
   
C++
High-level,
General-purpose
Language
   
*/

 



answered Apr 13, 2024 by avibootz
...