I was practicing on the topics of string. I am trying to concatenate two strings without using the strcat() function available in C.
This is the code that I came up with:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
int string_length();
void concatenate();
int main(void) {
char str[] = "Hello there";
char str2[] = "How are you";
char result[100];
int length;
length = string_length(str);
printf("%d\n", length);
concatenate(result, str, str2);
}
//Function which counts the number of characters of the string
int string_length(char str[]) {
int i = 0;
while (str[i] != '\0') {
i ;
}
return i;
}
//Function to concatenate the two strings
void concatenate(char result[], char str[], char str2[]) {
int i = 0;
while (str[i] != '\0') {
result[i] = str[i];
i;
}
int j = i 2, k = 0;
while (str2[k] != '\0') {
result[j] = str2[k];
k ;
j ;
}
printf("%s\n", result);
}
When I ran this code, this is what I get as the output -
11
Hello there
The concatenation function is not working properly as output should be-
11
Hello there How are you
I used the debugger and found that loops are working fine and as per the counter variable, The two strings should get concatenated.
CodePudding user response:
There are multiple problems in your code:
- in your concatenation function, you initialize
jtoi 2, whereas you should just usej = ias the first character of the second string should come just after the last character of the first andiwas incremented after each character copied fromstr. Note that you can just usei, there is no need forj. - you must set the null terminator at the end of
result. You can set at withresult[j] = '\0'; - you should output the concatenated string in
maininstead ofconcatenate(). - the prototypes of
string_length()andconcatenate()should include the argument types.
Also note that you should use size_t for the index types.
Here is a modified version:
#include <stdio.h>
#include <string.h>
size_t string_length(const char *s);
void concatenate(char *result, const char *str, const char *str2);
int main(void) {
char str[] = "Hello there";
char str2[] = "How are you";
char result[100];
size_t length;
length = string_length(str);
printf("%zu\n", length);
concatenate(result, str, str2);
printf("%s\n", result);
return 0;
}
//Function which counts the number of characters of the string
size_t string_length(const char *s) {
size_t i = 0;
while (str[i] != '\0') {
i ;
}
return i;
}
//Function to concatenate the two strings
void concatenate(char *result, const char *str, const char *str2) {
size_t i = 0;
while (str[i] != '\0') {
result[i] = str[i];
i;
}
size_t k = 0;
while (str2[k] != '\0') {
result[i] = str2[k];
k ;
i ;
}
result[i] = '\0';
}
CodePudding user response:
Problem lies in the fact that your result is empty (hence only 0s in it).
When you skip indexes at line 38, you have a null terminator in your string that stops the reading from printf.
You have to add manually a space if you do not want this comportment.
Add a line after your first while result[i] = ' '; and you should have your expected result.
However, as it has been said, you do not have to skip 2 spaces with j. Only one suffices.
CodePudding user response:
Because you skip two elements of the result array, it will look something like this after the second loop:
--- --- ----- --- --- --- --- --- --- --- ----
Index : | 0 | 1 | ... | 9 | 10| 11| 12| 13| 14| 15| ...
Character: | H | e | ... | r | e | ? | ? | H | o | w | ...
--- --- ----- --- --- --- --- --- --- --- ----
At index 11 and 12 there will be two unknown characters.
If you want to add a space you need to explicitly add it. And I recommend you keep the same i variable as the result array index both when adding the space and in the second loop:
// Needs to be define here since it's reused later
unsigned i;
// Copy the first string
for (i = 0; str[i] != '\0'; i) {
result[i] = str[i];
}
// The index i will be right after the last character of the first string
// Add the space there
result[i] = ' ';
// Increase i to the first position of the second string
i;
// Then copy the second string
for (unsigned k = 0; str2[k] != '\0'; k, i) {
result[i] = str2[k];
}
// Now add the string null terminator
result[i] = '\0';
After this result[11] will be a space, and directly following it (starting at result[12]) will be the second string.
