I want to add a variable number of spaces to a string in C, and wanted to know if there is a standard way to do it, before I implement it by myself.
Until now I used some ugly ways to do it:
This is one way I used:
add_spaces(char *dest, int num_of_spaces) {
int i;
for (i = 0 ; i < num_of_spaces ; i++) {
strcat(dest, " ");
}
}
This one is a better in performance, but also doesn't look standard:
add_spaces(char *dest, int num_of_spaces) {
int i;
int len = strlen(dest);
for (i = 0 ; i < num_of_spaces ; i++) {
dest[len + i] = ' ';
}
dest[len + num_of_spaces] = '\0';
}
So, do you have any standard solution for me, so I don't reinvent the wheel?
Once the character is equal to New-line ('\n'), ^ (XOR Operator ) gives false to read the string. So we use “%[^\n]s” instead of “%s”. So to get a line of input with space we can go with scanf(“%[^\n]s”,str);
For just a space, use ' ' . isspace would return affirmative for '\t' , which the OP doesn't want. Assuming he wants to check for all whitespace characters, and not just space.
C++ has a built-in method to concatenate strings. The strcat() method is used to concatenate strings in C++. The strcat() function takes char array as input and then concatenates the input values passed to the function.
There are various ways to concatenate strings. We will discuss each one by one. Method 1: Using strcat () function. The strcat () function is defined in “string.h” header file. The init and add string should be of character array (char*).
This is called concatenation: In the example above, we added a space after firstName to create a space between John and Doe on output. However, you could also add a space with quotes ( " " or ' ' ): A string in C++ is actually an object, which contain functions that can perform certain operations on strings.
Method 1: Using strcat () function. The strcat () function is defined in “string.h” header file. The init and add string should be of character array (char*). This function concatenates the added string to the end of the init string. Method 2: Using append () function. string& string::append (const string& str) str: the string to be appended.
Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator.
I would do
add_spaces(char *dest, int num_of_spaces) {
int len = strlen(dest);
memset( dest+len, ' ', num_of_spaces );
dest[len + num_of_spaces] = '\0';
}
But as @self stated, a function that also gets the maximum size of dest (including the '\0'
in that example) is safer:
add_spaces(char *dest, int size, int num_of_spaces) {
int len = strlen(dest);
// for the check i still assume dest tto contain a valid '\0' terminated string, so len will be smaller than size
if( len + num_of_spaces >= size ) {
num_of_spaces = size - len - 1;
}
memset( dest+len, ' ', num_of_spaces );
dest[len + num_of_spaces] = '\0';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With