Hello,
I am trying to combine to chars together, having a big of problem
what's the correct code?char char1;
char char2;
char1 = 'one';
char2 = ' two';
char1 = (char1[3] + char2[0];
printf("%s",char1);
Hello,
I am trying to combine to chars together, having a big of problem
what's the correct code?char char1;
char char2;
char1 = 'one';
char2 = ' two';
char1 = (char1[3] + char2[0];
printf("%s",char1);
'one' is not a single char, or a char string.
You cannot use the '+' operator to combine two strings.
Also, post code as CODE blocks not QUOTE blocks!
Look at this code, but be cautious about buffer overflow. More code would need to prevent overflow.
You need to study a good up to date book on the C Programming Language before writing C code!Code:#include <stdio.h> #include <string.h> int main(void) { char char1[20] = "one"; char char2[10] = " two"; // Or char *char2 = " two"; char2 is a pointer to a constant string // char1 must be a char array large enough to hold both strings strcat(char1, char2); printf("%s\n",char1); return 0; }
Last edited by rstanley; 10-13-2023 at 01:05 PM.
thanks,
so what exactly is '#include <string.h>' used for?
is it for 'strcat' can be used?
Last edited by WaterSerpentM; 10-13-2023 at 03:38 PM.
You need to include the correct header file for the declarations of Standard Library functions.
In this case you need to #include string.h for the declaration of the string concatenation function, strcat().
Please see the online manpage for strcat(), or if on Linux, use the command line command, "man strcat"
Once again, if you study a good up to date book on the C Programming Language, you would learn all about this an all the other details of the language!
ive tried to combine itself, but dont work
strcat(char3, char3);
[QUOTE=rstanley;1308879]This is legal but I don't know why you would concatenate char3 with char3. char 3 was not in your original code.
You would need to post the entire code for me to comment on it!
[CODE] blocks, NOTblocks!Code:char char3 [40] = "three"; char char4 [10] = " four"; strcat(char3, char4); printf("\n"); printf("%s",char3); strcat(char3, char3); printf("\n"); printf("%s",char3);
Last edited by WaterSerpentM; 10-13-2023 at 04:41 PM.
You have an 's' at the end of the last printf() statement.
A better way of doing this:
Code:#include <stdio.h> #include <string.h> int main(void) { char char3 [20] = "three"; char char4 [10] = " four"; char char5[30] = ""; // Always initialize the local variables strcat(char3, char4); printf("\n"); printf("%s",char3); strcpy(char5, char3); // A better method! strcat(char5, " "); // Print a space inbetween strcat(char5, char3); // printf("\n"); Not needed printf("%s\n",char5); return 0; }
There is a function called sprintf() that could simplify this even more.
I assume this is a homework assignment.
strcpy() copies an initial string into the destination, then strcat() adds strings to the end of the existing destination string.
Again, you need to study a good book that I will recommend in a later message.
WaterSerpentM:
If you are attempting to learn from painfully inadequate online tutorials, YouTube videos, or some book claiming to teach you in a short time, then THROW THEM AWAY!
Short of taking a course in C Programming from a qualified instructor, you need to study a good book on the C Programming Language, cover to cover, and do all the exercises at the end of each chapter! Choose one of the three listed below:
C Programming, A Modern Approach, 2nd Edition
Author: K. N. King
C Primer Plus, 6th Edition
Stephen Prata
C How to Program, 8/e
Deitel & Deitel
Studying one of these books, and writing code, you will have a much better understanding of the C Programming language.