1

I have simple function that should connect string but in different way. If i have one string "abc" and another "123" final string should be "a1b2c3".

Here is code

#define _CRT_SECURE_NO_WARNINGS 
#include <stdio.h>
#include <stdlib.h>

char *connStrings(char *s1, char *s2) {
    int s1L, s2L,i = 0,k=1,j=0;
    s1L = sizeof(s1);
    s2L = sizeof(s2);

    char* result = (char*) malloc((s1L + s2L));
    while (s1[i] != '\0') {
        result[j] = s1[i++];
        j = j + 2;
    }
    i = 0;
    while (s2[i] != '\0') {
        result[k] = s2[i++];
        k = k + 2;
    }
    result[s1L + s2L] = '\0';
    printf("\n %s \n", result);
}

int main() {
    connStrings("abcdefghi","123456789");
}

So what's the problem? the final output of this program is still the same "a1b2c3d4e5f6g7h8"
It ignores i and 9 somehow. Even if I add more chars into both strings it still prints the same
"a1b2c3d4e5f6g7h8". I will be thankful for any help or advice.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Sheldon
  • 114
  • 10

2 Answers2

1

Use proper function return type and strlen instead of sizeof

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include <stdlib.h>
#include<string.h>

void connStrings(char *s1, char *s2) {
    int s1L, s2L, i = 0, k = 1, j = 0;
    s1L = strlen(s1);
    s2L = strlen(s2);

    char *result = (char *) malloc((s1L + s2L + 1 )); // extra one for the `null` terminator `\0`
    while (s1[i] != '\0') {
        result[j] = s1[i++];
        j = j + 2;
    }
    i = 0;
    while (s2[i] != '\0') {
        result[k] = s2[i++];
        k = k + 2;
    }
    result[s1L + s2L + 1] = '\0';
    printf("\n %s \n", result);
    // free memory when you're done
    free(result);

}

int main() {
    connStrings("abcdefghi", "123456789");
    return 0; // or use void
}
some user
  • 1,693
  • 2
  • 14
  • 31
  • should add 1 to the malloc as well. – Jiminion Feb 07 '20 at 15:05
  • And here `result[s1L + s2L] = '\0';` should not be +1 too? – Sheldon Feb 07 '20 at 15:11
  • I am asking because when i add + 1 it adds "=" at the end of the string and i get error "heap corruption detected". (when i ctrc and ctrlv your code i get error) But i understand that my main problem was in strlen so i mark this as solved) Thank you. – Sheldon Feb 07 '20 at 15:15
1

When you pass an array as an argument to a function, what you're really passing is a pointer to the first element of the array.

sizeof will not return the length of the buffer in this case, but the size of a pointer.

Fortunately, C strings have the useful property of being terminated with a null character ('\0'), which allows functions like strlen to give you the actual length of the string, even when all you have is a pointer and not the array. strlen works similar to this:

size_t strlen(char *s)
{
    size_t ret = 0;
    while(*s) ret++, s++;
    return ret;
}

Even when calculating the length of a string when you have an actual array, you still shouldn't use sizeof because that will return the entire size of the buffer, which may not be the length of the string within it.

Consider: char s[100] = "Hello World";

sizeof s would be 100; strlen(s) would return 11.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85