Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C: How to append/concatenate 'x' spaces to a string

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:

  • Please assume that before I called any of the below functions, I took care to have enough memory allocated for the spaces I want to concatenate

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?

like image 803
SomethingSomething Avatar asked Feb 18 '14 13:02

SomethingSomething


People also ask

Can we give space in string in C?

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);

How do you add spaces in C?

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.

How do I concatenate a char to a string in C++?

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.

How to concatenate strings in C++?

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*).

How do you add a space to a string in C++?

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.

How to append a string to a string in C++?

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.

What is concatenation in JavaScript?

Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator.


1 Answers

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';
}
like image 110
Ingo Leonhardt Avatar answered Oct 23 '22 01:10

Ingo Leonhardt