How to check if string is empty in C++

2 Answers

0 votes
#include <iostream>

int main() {
    std::string s = "";
    
    if (s.empty()) {
        std::cout << "empty";
    } else {
        std::cout << "not empty";
    }
    
    return 0;
}



/*
run:

empty

*/

 



answered Feb 26, 2021 by avibootz
0 votes
#include <iostream>

int main() {
    std::string s = "";
    
    if (!s.length()) {
        std::cout << "empty";
    } else {
        std::cout << "not empty";
    }
    
    return 0;
}



/*
run:

empty

*/

 



answered Feb 26, 2021 by avibootz

Related questions

1 answer 222 views
222 views asked Apr 13, 2020 by avibootz
1 answer 292 views
1 answer 300 views
1 answer 267 views
1 answer 380 views
1 answer 294 views
...