TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ String resize() function

C++ String resize function tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ String resize()

This function is used to resize the string to the length of k characters.

Syntax

Consider a string object str. To resize the string object, syntax would be :

str.resize(k,c);

Parameters

This function contains two parameters.

  • k : k is number of characters specified in the first parameter. It resizes the string such that string contains k characters.
  • If k is shorter than the length of the string, string length shortened to the length specified by k, removing all the characters beyond k.

    If k is larger than the length of the string, string length extended to the length specified by k.

  • c : c is a new character to be added in a new space, if k is larger than the length of the string. This is optional parameter.

Return value

It does not return any value.

Example 1

When k is shorter than length of the specified string.

#include<iostream>
using namespace std;
int main()
{
string str= "TheDeveloperBlog";
cout<<"String is :"<<str<<?\n?;
str.resize(4);
cout<<"After resizing, string is "<<str;
return 0;
 } 

Example 2

When k is greater than the length of the specified string.

#include<iostream>
using namespace std;
int  main()
{
string str ="TheDeveloperBlog";
cout<<"String value is :"<<str<<'\n';
str.resize(19,"tutorial");
cout<<"After resizing, string value is :"<<str;
return 0;
} 

Output:

String value is TheDeveloperBlog
After resizing, string value is TheDeveloperBlog tutorial 

Next TopicC++ Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf