0

I have two strings in C.

char* lexeme = "this is an example";
char c = 'a';

I want to concat the two strings to have as a result this:

"this is an examplea"

I've already tried using strcpy and strcat, but it gives an error because the second char is not of type char*

  • Do you know how to copy a string? Do you know how to modify a string? – Beta Oct 19 '22 at 01:21
  • Does this not awnser? https://stackoverflow.com/questions/308695/how-do-i-concatenate-const-literal-strings-in-c – mont_ Oct 19 '22 at 01:23
  • 1
    [Check the link, I think its the same issue](https://stackoverflow.com/a/8569479/20277639) Happy Coding :) – Arjein Oct 19 '22 at 01:27
  • 3
    You *do not* have two strings. You have one string and one individual `char`. The latter is not a string, because string's are *null terminated* sequences of `char`s. – John Bollinger Oct 19 '22 at 01:36
  • 1
    I don't agree with the duplicate targets. The first shows how to concatenate two string literals, which isn't the case here. The second shows to prepend a character to a string, while in this case the OP actually wants to append a character. – Lundin Oct 19 '22 at 13:17

3 Answers3

3

Problem 1: lexeme is a pointer to read-only memory so you can't store or change anything at that location.
Problem 2: c is not a string but a single character.

You can solve it in the following manner:

  • Find the string length of lexeme and store it in a variable old_length.
  • Allocate a "large enough" copy of the string lexeme, for example by using malloc + strcpy. "Large enough" means room for the original string, an additional character and also the null terminator at the end.
  • In the new allocated string (lets call it newstr), write the character to index newstr[old_length]. This is the location where the null terminator is currently located, so it will be overwritten.
  • In the new allocated string, write a null terminator to index newstr[old_length+1].
Lundin
  • 195,001
  • 40
  • 254
  • 396
3

You can write your own routine to do that:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char *appended(char const* src, char const ch) {
    size_t const size = strlen(src);
    char* copy = malloc(size + 2);
    if (!copy) {
        return 0;
    }

    memcpy(copy, src, size);
    copy[size] = ch;
    copy[size + 1] = 0;
    return copy;
}

int main() {
    char* str = appended("Hello Worl", 'd');

    if (!str) {
        return EXIT_FAILURE;
    }

    puts(str);
    free(str);
    return EXIT_SUCCESS;
}
Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
  • There's a reason I wrote a description over how to write the program rather than handing out the complete source code. Those interested in learning will benefit from it, those who just want their homework done won't. The OP has not posted any of their attempt to solve this themselves, so giving them the complete source code only encourages more low effort homework dumps. – Lundin Oct 19 '22 at 13:39
  • @Lundin You may be right. Forgetting this particular OP for the moment, how about those interested in learning and prefer samples over written descriptions? – Aykhan Hagverdili Oct 19 '22 at 13:57
  • @Lundin, an argument could be made that this example is clearer about handling null return from `malloc()` and of the need to `free()` the result, so I think both are valuable. – Toby Speight Oct 19 '22 at 13:57
  • It's all about learning the various language features one at a time. Characters. Arrays. Pointers. Strings. String literals. String copying. Dynamic allocation. One doesn't need to learn about all of these at the same time in the same example. Rather, a task such as this is likely all about applying previously gained knowledge. – Lundin Oct 19 '22 at 14:08
-1

I like the above code better. But I wrote something quickly.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    char lexeme[] = "this is an example";
    char c = 'a';
    int len_of_lexeme = strlen(lexeme);

    char *concat_str = malloc(len_of_lexeme + 2);

    strcpy(concat_str, lexeme);
    concat_str[len_of_lexeme] = c;
    concat_str[len_of_lexeme + 1] = '\0';
    

    printf("concat_str = [%s]\n", concat_str);

    free(concat_str);
}
bekici
  • 68
  • 10