#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int toggleChars (char* string, char* letters){
int count = 0;
char* scurrent = string;
while(*scurrent != '\0'){
char* lcurrent = letters;
while(*lcurrent != '\0'){
if(*scurrent == *lcurrent){
*scurrent = '0';
count += 1;
}
lcurrent += 1;
}
scurrent += 1;
}
return count;
}
int main(){
char* str = malloc(50);
char* letters = malloc(20);
str = "Hi how R U today?";
letters = "HhiR";
int x = toggleChars(str, letters);
printf("str: %s\ncount: %d", str, x);
return 0;
}
This is just a testing function I made to study for an upcoming exam in C programming. When I compile this and run it, it gives me a segfault. I have deduced with some testing that it is caused by the line
*scurrent = '0';
So it means that I should not change the character at that memory location. But then if I do want to change it, what would I have to do?