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

51,811 answers

573 users

How to insert spaces between words that start with capital in a string with C++

2 Answers

0 votes
#include <iostream>
  
using namespace std;
  
string insert_spaces(string s) { 
    string tmp = "";
    int len =  s.length();
    for (int i = 0; i < len; i++) {
         if (s[i] >= 'A' && s[i] <= 'Z' && i != 0) { 
             tmp.append(" ");
         }
         tmp.append(1, s[i]);
    }
    return tmp;
} 
  
int main() {
    string s = "PythonJavaPascalF#C++C#";
  
    s = insert_spaces(s);
      
    cout << s;
}
  
  
  
/*
  
Python Java Pascal F# C++ C#
  
*/
 

 



answered Jan 15, 2020 by avibootz
edited Jan 15, 2020 by avibootz
0 votes
#include <iostream>
     
using namespace std;
     
string insert_space(string s) { 
    for (int i = 1; i < s.length(); i++) {
         if (s[i] >= 'A' && s[i] <= 'Z') { 
             s.insert(i, " ");
             i++;
         }
    }
    return s;
} 
     
int main() {
    string s = "PythonJavaPascalC#C++F#";
     
    s = insert_space(s);
    
    cout << s;
}
     
     
     
/*
     
Python Java Pascal C# C++ F#
     
*/

 



answered Jan 15, 2020 by avibootz
edited Jan 16, 2020 by avibootz
...