-
array trimming...
Lets say I have an array like this:
Code:
char str[30] = " Hello ";
How would I trim off all the leading and trailing whitespaces?
I tried doing this with a cnt of all non ' ' then making a new array with the size of cnt. Then I noticed that I can't create an array with a variable. So how should I go about this?
-
Use a for loop to count the number of leading spaces
Use another for loop to count the trailing spaces
Use strncpy() to copy the bit in between
-
The problem you ran into is that unless you use dynamic memory allocation, the size of the array must be constant. Assuming you have not yet studied dynamic memory allocation, you can get around this using the std::string class in the <string> header. A quick reference on what you find in the class here: cpprefence.com's strings