C++ String resize() function
Last Updated : 17 Mar 2025
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 :
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.
- 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.
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.
Return value
It does not return any value.
Example 1
When k is shorter than length of the specified string.
Example 2
When k is greater than the length of the specified string.
Output:
String value is javatpoint After resizing, string value is javatpoint tutorial