Okay, so my problem is I have created a replace function that allows an argument to choose which node to be replaced and enter a new one. I have a code that works fine, but it uses integers and I need it to use strings instead. My code with strings will not work and terminates the program after I enter what I want to replace the chosen word with. I will put the two codes below.
Code:
void rep(int i) {
int old, n;
struct node* temp;
temp = root;
old = i;
printf("\nEnter what you want to replace with: ");
scanf("%d", &n);
if(temp == NULL) {
printf("No such index\n");
return NULL;
}
while (temp != NULL) {
if(temp->data == old) {
temp->data = n;
printf("Replaced\n");
}
temp = temp->next;
}
}
And this is the code I need fixed:
Code:
// Command replace
void rep(char* i) {
char old, n;
struct node* temp;
temp = root;
strcmp(old,i);
printf("\nEnter what you want to replace with: ");
scanf("%s", &n);
if(temp == NULL) {
printf("No such index\n");
return NULL;
}
while (temp != NULL) {
if(strcmp(temp->text, old) == 0) {
strcpy (temp->text, n);
printf("Replaced\n");
}
temp = temp->next;
}
}