How to calculate string length without spaces in C++

1 Answer

0 votes
#include <iostream>
#include <string>

int calculateLengthWithoutSpaces(std::string str) {
    int lengthWithoutSpaces = 0;
    
    for (char ch : str) {
        if (ch != ' ') {
            lengthWithoutSpaces++;
        }
    } 
    
    return lengthWithoutSpaces;
}

int main() {
    std::string str = "C++ is a leading programming language used in game development";

    std::cout << calculateLengthWithoutSpaces(str) << std::endl;
}


    
/*
run:
     
53
   
*/

 



answered Jan 11, 2025 by avibootz

Related questions

1 answer 91 views
1 answer 126 views
1 answer 118 views
1 answer 124 views
1 answer 125 views
1 answer 98 views
1 answer 190 views
...