string - Character replacement and substitution in C -


i'm trying write code replace character in string user selects character he/she does. eg string london if user picks o , a output should landan.

here code:

#include <stdio.h> #include <string.h>  #define maxlen 100  int function2(char str[], char drop, char sub) {     int = 0; int num = 0;     while (str != null) {         if (str[i] == drop) {             str[i] = sub;             num++;         }         i++;     }     return num; }  int main() {     char d, s;     char my_string[maxlen];      printf("input string\n");     scanf("%s", &my_string);     printf("input character want drop\n");     scanf(" %c", &d);     printf("now character want substitute\n");     scanf(" %c", &s);     function2(my_string, d, s);     printf("the string %s\n", my_string);     return exit_success; } 

it works until point print altered string. segmentation fault (core dumped). note code function not mine (i found on website, owner of original code function2- thank in advance). appreciated!

first of all, should avoid using scanf. if you're interested reason , alternatives click here.

but problem

while(str != null) 

is infinite loop, because pointer won't become null

while(str[i] != '\0') 

should trick. checks each time if you've arrived @ end of string.


Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -