I'm pretty sure it has to do with my use of calloc() but I don't understand why. The objective of the program is for char* C
to contain characters of char* A
that are not in char* B
.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
char* diff(char* A, char*B);
int main() {
char* A = {'q','g','e','\0'};
char* B = {'a','d','e','\0'};
char* C = diff(A,B);
printf("%s", C);
free(C);
return(0);
}
int search(char* S, char c) {
int i=0;
while( S[i]!='\0' ){
if( S[i]==c ) break;
i++;
}
if( S[i]=='\0' ) return -1;
else return i;
}
char* diff(char* A, char* B) {
int i;
char* C = calloc(strlen(A), sizeof(char));
assert(C != NULL);
int lengthC = 0;
for (i = 0; i < strlen(A); i++) {
if (-1 != -1) {
C[lengthC] = A[i];
lengthC++;
}
}
C[lengthC] = '\0';
return C;
}