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

51,935 answers

573 users

How to resize string in C++

3 Answers

0 votes
#include <iostream>
  
int main() {
    std::string s = "c++";
 
    std::cout << s << " : " << s.size() << "\n";
 
    s.resize(s.size() + 3, '+');     
     
    std::cout << s << " : " << s.size() << "\n";
}
  
  
  
/*
run:
  
c++ : 3
c+++++ : 6
  
*/

 



answered Dec 9, 2020 by avibootz
edited Apr 23, 2024 by avibootz
0 votes
#include <iostream>
   
int main() {
    std::string s = "c++";
  
    std::cout << s << " : " << s.size() << "\n";
  
    s.resize(s.size() + 5);     
      
    std::cout << s << " : " << s.size() << "\n";
     
    s[3] = ' '; s[4] = '1'; s[5] = '7';
     
    std::cout << s << " : " << s.size() << "\n";
}
   
   
   
/*
run:
   
c++ : 3
c++ : 8
c++ 17 : 8
   
*/

 



answered Dec 9, 2020 by avibootz
edited Apr 23, 2024 by avibootz
0 votes
#include <iostream>

using std::cout;
using std::endl;
using std::string;
 
int main()
{
    string s = "c c++ java php";
 
    cout << s.size() << endl;
 
    s.resize(30);
 
    cout << s.size() << endl;
}
 
 
/*
run:
 
14
30
 
*/

 



answered Apr 23, 2024 by avibootz

Related questions

4 answers 87 views
87 views asked Oct 23, 2025 by avibootz
2 answers 184 views
184 views asked Jul 22, 2020 by avibootz
1 answer 178 views
4 answers 546 views
2 answers 170 views
170 views asked May 4, 2018 by avibootz
1 answer 203 views
...