Hello, I'm trying to take a C array of char's and trying to take all of the spaces out of it. Yet I keep running into a segmentation fault. Can anyone help me here?
#include <stdio.h>
#include <stdlib.h>
int strlen(char* Str) {
int toret=0;
int i=0;
char tmp = ' ';
while (tmp != '\0') {
toret += 1;
tmp = Str[i];
i++;
}
return toret;
}
char* deblank(char* OrgStr) {
char* chr1 = OrgStr;
char* chr2;
int len = strlen(OrgStr);
char* NewStr = (char*) malloc(len + 1);
int i=0;
int j=0;
while (len >= 0) {
*chr1 = OrgStr[j];
if (*chr1 != ' ') {
NewStr[i] = *chr1;
i++;
}
j++;
len--;
}
NewStr[i] = '\0';
return NewStr;
}
int main(void) {
printf("TEST\n");
char* test = "Hello there";
char* b = deblank(test);
printf("%s\n%s\n", test, b);
return 0;
}