I am having trouble concatenating strings in C without library function. I tried the following:
#include <stdio.h>
struct word {
char *str;
int wordSize;
};
void concat(struct word words[], int arraySize, int maxSize) { // word array, its size, and max size given
char result[maxSize];
int resultSize = 0;
struct word tmp;
for (int i = 0; i < arraySize; i++) {
tmp = words[i];
for (int j = 0; j < words[i].wordSize; j++, resultSize++) {
result[resultSize + j] = tmp.str[j];
}
}
puts(result);
}
For example, if the struct array words
contain [{"he", 2}, {"ll", 2}, {"o", 1}]
, the result
should be hello
. However, this code prints h l o
where the second and fourth letters are questionmark. Can anyone help me debug this?
Keep it simple and the bugs will fix themselves. Don't mix up the position in the result
buffer with the loop iterators. No need for temporary variables.
#include <stdio.h>
typedef struct {
char *str;
int wordSize;
} word;
void concat(word words[], int arraySize, int maxSize) {
char result[maxSize];
int count=0;
for(int i=0; i<arraySize; i++)
{
for(int j=0; j<words[i].wordSize; j++)
{
result[count]= words[i].str[j];
count++;
}
}
result[count] = '\0';
puts(result);
}
int main()
{
word w[3] = { {"he", 2}, {"ll", 2}, {"o", 1} };
concat(w, 3, 128);
}
In this for loop
for (int j = 0; j < words[i].wordSize; j++, resultSize++) {
result[resultSize + j] = tmp.str[j];
}
you are incrementing resultSize
and j
simultaneously while you need to increase only the variable j
and after the loop increase the variable resultSize
by j
.
But in any case the function is wrong because there is no check that resultSize
is less than maxSize
.
And moreover the built string is not appended with the terminating zero '\\0'
. As a result this statement
puts(result);
invokes undefined behavior.
There is no need to create the variable length array
char result[maxSize];
just to output the concatenated string.
The function can be declared and defined the following way as it is shown in the demonstrative program below.
#include <stdio.h>
struct word {
char *str;
int wordSize;
};
void concat( const struct word words[], size_t arraySize, size_t maxSize )
{
for ( size_t i = 0, j = 0; i < maxSize && j < arraySize; j++ )
{
for ( size_t k = 0; i < maxSize && k < words[j].wordSize; i++, k++ )
{
putchar( words[j].str[k] );
}
}
putchar( '\n' );
}
int main(void)
{
struct word words[] =
{
{ "he", 2 }, { "ll", 2 }, { "o", 1 }
};
const size_t arraySize = sizeof( words ) / sizeof( *words );
concat( words, arraySize, 5 );
return 0;
}
The program output is
hello
You have a couple of problems here. result[resultSize + j]
means you are effectively skipping over half the characters in result. Secondly, if you copy the entire string, including the null character, puts
will stop printing the result string at the end of the first word itself. So you need to skip copying the string terminator and put it in at the end of your whole concatenated string.
#include <stdio.h>
struct word {
char *str;
int wordSize;
};
void concat(struct word words[], int maxSize) { // word array and max size given
char result[maxSize];
int resultSize = 0;
struct word tmp;
for (int i = 0; i < 3; i++) {
tmp = words[i];
// Keep copying the words to result, one after the other
// But drop the last NULL character
for (int j = 0; j < (words[i].wordSize - 1); j++, resultSize++) {
result[resultSize] = tmp.str[j];
}
}
// Put the NULL character in after all words have been copied
result[resultSize] = 0;
puts(result);
}
struct word mywords[] =
{
{ "TestWord", sizeof("TestWord")},
{ "TestWord2", sizeof("TestWord2")}
};
int main(void)
{
concat(mywords, 100);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.