How to find the index of the last space in string with C++

1 Answer

0 votes
#include <iostream>

int main() {
    std::string s = "C++ is a general-purpose programming language";
    char ch = ' ';
 
    size_t index = s.find_last_of(ch);
 
    if (index == std::string::npos) {
        std::cout << "Not found";
    } else {
        std::cout << "Found at index: " << index;
    }
    
    return 0;
} 
   
   

   
/*
run:
     
Found at index: 36
     
*/

 



answered Feb 16, 2022 by avibootz

Related questions

1 answer 84 views
1 answer 87 views
1 answer 92 views
1 answer 89 views
1 answer 79 views
...