Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenating a char into a string

I am trying to read a string from the console. But I want to read it char by char. And I'm having trouble with concatenating the char into the string AND with breaking the loop. Here is the code:

char* createString(){
    char c;
    char *string;
    int x=0;

    string = (char*) calloc(1, sizeof(char));
    do{
        c = getche();
        if(c != '\n'){
            x++;
            realloc(string, sizeof(char)*x);
            strcat(string, &c);
        };
    }while(c != '\n');
    return string;
};

When I run this code, each concatenation adds 3 chars, instead of just 1. It's like is accessing non-allocated memory... (For example, if I press a, the final string is a%T. Then, if I press another key, s for example, string becomes a%Ts%T)

And when I press Enter, it goes into the if and doesn't get out of the loop.

I have no clue of why and what is going on...


EDIT


Based on other tries and responses until now, I changed my code and now it's like this:

char* digitarString(){
    char c[2];
    char *string;

    string = (char*) calloc(1, sizeof(char));
    do{
        c[0] = getche();
        c[1] = '\0';
        if(c[0] != '\n'){
            strcat(string, c);
        };
    }while(c[0] != '\n');
    return string;
};

BUT, there are still two issues...

  • The code works, but I think that it's writing in non-allocated memory.
  • When I press Enter it still doesn't work. It keep entering the loop and if.

Forget about the Enter... I changed it...

c[0] = getche();

to

scanf("%c", &c[0]);

and worked awesomely well.

like image 664
Bestknighter Avatar asked May 20 '13 15:05

Bestknighter


People also ask

How do I concatenate a char to a string?

You can use String(char[] value) constructor to convert char array to string.

Can I append a char to a string?

Use the strncat() function to append the character ch at the end of str. strncat() is a predefined function used for string handling. string.

Can you 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.

Can we concatenate string and char in C?

In C, "strings" are just plain char arrays. Therefore, you can't directly concatenate them with other "strings".


1 Answers

Ok here is the solution

 strcat(string, &c);

change this to

strncat(string, &c,1);

now the answer to the question why ?

well first of call the below statement

c = getche();

will scan a value for us and will place in variable called c

now lets consider the variable is placed in an arbitrary memory location x

    c
+-----------+------------+------------+-----------+------------+------------+
|     a     |            |            |           |            |            |
+---------- +----------  +----------  +---------- +--------- - +---------- +  
  x = &c       x+1             x+2            ......

now to the next important statement

strcat(string, &c);

the second argument above is supposed to be a string means a NULL termination at the end but we can not guarantee that x+1 location is NULL and if x+1 is not NULL then the string will be more than a single character long and it will end up appending all those characters to your original string hence garbage.

I hope it's clear now...

P.S - if you have access to gdb you can check practically..

like image 192
asio_guy Avatar answered Oct 19 '22 02:10

asio_guy