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.
Related
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.
This question already has answers here:
How should character arrays be used as strings?
(4 answers)
Closed 1 year ago.
I'm trying to practice declaring Strings but I'm not getting correct output.
#include <stdio.h> //Im using vscode and gcc
int main()
{
char k[]= "prac c";
char l[6]= "stop c"; //first initializing as size 6
char m[6]= "nice c";
printf("%s \n%s \n%s",k,l,m);
return 0;
}
output: prac c
stop cprac c
nice cstop cprac c
But when I change the size from 6 to 7 this does not happen.
#include <stdio.h>
int main()
{
char k[]= "prac c";
char l[7]= "stop c"; //changing size to 7
char m[7]= "nice c"; //changing size to 7
printf("%s \n%s \n%s",k,l,m);
return 0;
}
output: prac c
stop c
nice c
The string "stop c" is really seven characters long, including the ending null-terminator. This goes for all your strings.
If there's no space in your arrays for the terminator then it won't be added, and the arrays are no longer what is commonly called strings.
Using such arrays as string will lead the code to go out of bounds of the arrays and give you undefined behavior.
"prac c", "stop c", "nice c" all occupy 7 bytes separately, as there's a terminating \0 for a string literal.
This question already has answers here:
Given a starting and ending indices, how can I copy part of a string in C?
(3 answers)
Closed 4 years ago.
I have a string
str = "hello"
I want to make a new string which is the first two digits of str "he".
How do I do this in C?
Use strncpy(), like this:
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "hello";
char dest[3]; // Two characters plus the null terminator
strncpy(dest, &src[0], 2); // Copy two chars, starting from first character
dest[2] = '\0';
printf("%s\n", dest);
return 0;
}
Output:
he
This question already has answers here:
How can I convert an int to a string in C?
(10 answers)
Closed 7 years ago.
I'm trying to convert an integer to a string in C but the current code doesn't make it.
I'm not seeking to display it in the screen, so all the functions printf, sprintf... are irrelevant.
int X = 15;
char *T;
T = (char*)X;
// Expected result : "15"
Can anyone help please ?
Thanks.
Not displaying it to screen doesn't invalidate functions like sprintf() since they literally "print to string".
int X = 15;
char buffer[10];
memset(&buffer, 0, sizeof(buffer)); // zero out the buffer
sprintf(buffer, "%d", X);
// Expected result : "15"
printf("contents of buffer: %s\n", buffer);
sprintf will print to a string, not the screen.
It's exactly what you're looking for.
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.