简体   繁体   中英

What is another alternative to strcat and strncat functions in C?

This is the specific section of code where I am facing issues using both strcat() and strncat() functions to concatenate two strings.

The strcat() function is declared as char *strcat(char *dest, const char *src) and strncat() as char *strncat(char *dest, const char *src, size_t n) , however both of them give issues when the second parameter is a single character from string, ie, which does not end with '\\0' . I need to concatenate the a character to a string.

So is there any alternative to these two functions or is there any way to make these functions work for me?

    char *cipher_str = (char *)malloc(sizeof(char) * 26);
    for (int j = 0; j < col; j++) {
        for (int i = 0; i < col; i++) {
            if (min == cipher[0][i] && (done[i] != 1)) {
                done[i] = 1;
                for (int k = 0; k < rows; k++)
                    strcat(cipher_str, cipher[i][k]);
                }
            }
            ...

Th easiest way here is just to append the character "manually" to the string, for example:

    int m=0;

    ....
        cipher_str[m++]=cipher[i][k];
    ....
    cipher_str[m]='\0';

Since you are using strcat to append 1 character, you can use this function.

void strcat(char* dest, char src)
{
    int size;
    for(size=0;dest[size]!='\0';++size);
    dest[size]=src;
    dest[size+1]='\0';
}

And you mentioned that you have '\\0' at end of your 'cipher_str', i used it to determine length.

What is another alternative to strcat and strncat functions in C?

In-line code can work well with size_t m like with @Paul Ogilvie answer.

As stand-alone functions:

char *strcat_c(char *s, char c) {
  size_t len = strlen(s);
  s[len++] = c;
  s[len] = '\0';
  return s;
}

char *strncat_c(char *s, char c, size_t n) {
  char sc[2] = { c, '\0' };
  return strncat(s, sc, n);
}

// or with C11 compound literals

char *strncat_c(char *s, char c, size_t n) {
  return strncat(s, (char [2])  { c, '\0' }, n);
}

Convert the single char into a string with length 1, then use strcat()

#include <string.h>

void appendchar(char *x, char y) {
    char z[2];
    z[0] = y;
    z[1] = 0;
    strcat(x, z);
}

char data[1000] = "foo";
appendchar(data, 'b');
appendchar(data, 'a');
appendchar(data, 'r');
puts(data);

beware buffer overflows

There are multiple problems with your code:

  • cipher_str is allocated with malloc() , so it is uninitialized. Passing it as the destination string for strcat() has undefined behavior as it does not contain a proper string.

  • Passing a char as second argument to strcat is incorrect. If you insist on using a function to append a single char , you could write:

     char *cipher_str = calloc(26); for (int j = 0; j < col; j++) { for (int i = 0; i < col; i++) { if (min == cipher[0][i] && done[i] != 1) { done[i] = 1; for (int k = 0; k < rows; k++) strncat(cipher_str, &cipher[i][k], 1); } } ... 

But it is overkill to call a function for this simple task and you do not check if cipher_str is long enough.

Here is a much simpler solution with an index variable:

char *cipher_str = (char *)malloc(sizeof(char) * 26);
int ii = 0;
for (int j = 0; j < col; j++) {
    for (int i = 0; i < col; i++) {
        if (min == cipher[0][i] && (done[i] != 1)) {
            done[i] = 1;
            for (int k = 0; k < rows; k++) {
                if (ii < 25) {
                    cipher_str[ii++] = cipher[i][k];
                } else {
                    // cipher_str is too short for the job.
                }
            }
        }
        cipher_str[ii] = '\0';
        ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM