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,989 questions

51,934 answers

573 users

How to find the smallest word in a string with C++

1 Answer

0 votes
#include <iostream>
#include <sstream>
#include <climits>
 
std::string GetSmalletWord(std::string str) {
    std::stringstream iss(str);
    std::string smallest;
    int min = INT_MAX; 
      
    std::string word;  
    while (iss >> word) {
        if (word.size() < min) {
            min = word.size();
            smallest = word;
        }
    }
     
    return smallest;
}
 
int main() {
    std::string s = "c++ java c python java c# php c++";
 
    std::cout << GetSmalletWord(s);
}
 
 
 
 
 
/*
run:
 
c
 
*/

 



answered Nov 11, 2022 by avibootz
edited Nov 17, 2022 by avibootz

Related questions

2 answers 158 views
1 answer 65 views
1 answer 135 views
2 answers 123 views
1 answer 67 views
1 answer 77 views
3 answers 217 views
...