Concatenating strings (2) [duplicate] - c

This question already has answers here:
How do I concatenate two strings in C?
(12 answers)
Closed 8 years ago.
char *val1 = "/root";
char *val2 = "/p";
val1 = val1+val2;
I want to add 2 char pointer value and assign it to 1st value. above is the code snippt.

It can't be done that way. Since you have two pointers, trying to add them will try to add the pointers themselves, not manipulate what they point at. To concatenate the two strings, you need to have/allocate a single buffer large enough to hold both:
char *both = malloc(strlen(val1) + strlen(val2) + 1);
if (both != NULL) {
strcpy(both, val1);
strcat(both, val2);
}

Use strcat or strncat functions to concatenate strings. C has no string concatenation operator.

Related

String to array elements in C [duplicate]

This question already has answers here:
Split string with delimiters in C
(25 answers)
Why no split function in C? [closed]
(1 answer)
Closed 3 months ago.
I need a function that works in C like split(' ') in python. I have to add words from a string as array elements (so I have to "cut" the string at every space characters and put the words in an array). I have no idea how to do it simply.
I tried for example the strstr() but it's just find the first space in the sentence, but I have 3 spaces.
strtok
"String tokenize"
int main ()
{
char str[] ="one two three";
char * word;
word = strtok (str," ");
while (word != NULL)
{
printf ("%s\n",word);
word = strtok (NULL, " ");
}
return 0;
}
Be careful though, it modifies the input string. It replaces any "delimiter" chars with \0s.
It's also non-reentrant. It keeps state in some global variable so you can't interweave tokenizations of 2 different strings, not even talking about multi-threading.

How to extract the first X characters of a string and use them into another in C [duplicate]

This question already has answers here:
How should character arrays be used as strings?
(4 answers)
Closed 3 years ago.
I have made this function to extract the first 2 characters of a string:
string get_salt(string hash)
{
char pre_salt[2];
for (int i = 0; i < 2; i++)
{
pre_salt[i] = hash[i];
}
string salt = pre_salt;
printf("%s\n", salt);
return(salt);
}
But when I run it with a string that has "50" (is the example I'm using) as the first 2 characters, I get this output:
50r®B
And to be honest I have no idea why is it adding the 3 extra characters to the resulting string.
You are missing the string's NUL terminator '\0' so it keeps printing until it finds one.
Declare it like:
char pre_salt[3]={0,0,0};
And problem solved.

Removing spaces in a char array [duplicate]

This question already has answers here:
Function to remove spaces from string/char array in C
(6 answers)
Closed 3 years ago.
I want to remove spaces from an array and rearrange the values of the array.
array[]="1 2 6 9 5";
I want the values of array like
array[]="12695"
There are tons of ways to whip up a simple solution to remove spaces from a string. Here's a little example I came up with using pointer iteration:
void remove_spaces(char * str){
char * back = str;
do{
if(*back != ' ')
*str++ = *back;
}while(*back++ != '\0');
}
This code uses two pointers to iterate across the given string. At each step of the loop, the back pointer is checked against the null character (to see if the end of the string has been reached) and incremented. In the body of the loop, the value from the back pointer is copied into the front pointer whenever back is not pointing to a ' '. str is also incremented right after this copy from *back to *str is made, using the post-increment (++) operator.

How concatenate string and variable to a string in C [duplicate]

This question already has answers here:
How do I concatenate const/literal strings in C?
(17 answers)
Closed 4 years ago.
I need to concatenate a string and a variable to fill another string:
#include <stdio.h>
int main(){
char *te1;
char *te2;
char *tabela[2];
te1 = "text 1";
te2 = "text 2";
tabela[0] = "text 0, " + te1 + ", " + te2;
printf("%s\n", tabela[0]);
return 0;
}
expected result:
text 0, text 1, text 2
Ooof! You need to read up on C, pointers and arrays.
Firstly, you need to allocate some space, either using malloc and assign to a pointer or char buffer[200]
Next be aware that you cannot add strings like other languages. You need to either strcat or one of its variants or sprintf(buffer, "text 0 %s, %s",te1, te2);
There's some clues but with no ill intent meant, you really do need to ready up and understand some fundamentals.

Expanding char by some more chars in C ( no overwriting allowed ) [duplicate]

This question already has answers here:
How do I concatenate const/literal strings in C?
(17 answers)
C appending char to char*
(6 answers)
Closed 9 years ago.
I would like to save more chars in ch[500] then it is already there. I dont want to lose the chars I've saved there before.
Something that would work like this:
ch = ch + ’nextch’;
You can use strcat() or strncat() to Concatenate two strings.
for example
char ch[100];
strcpy(ch,"hello");
strcat(ch," world");
if you want to append only one character
char str[100];
strcpy(str,"hello");
char ch='a';
char buf[2];
sprintf(buf,"%c",ch);
strncat(str,sizeof str, buf);
or
size_t length= strlen(str);
str[strlen(str)]=ch;
str[length+1]='\0';

Resources