1

I'm trying to write a code which will replace character in the string user selects with character he/she does. Eg string london if user picks o and a then the output should be landan.

Here is the code:

#include <stdio.h>
#include <string.h>

#define MAXLEN 100

int function2(char str[], char drop, char sub) {
    int i = 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 your string\n");
    scanf("%s", &my_string);
    printf("Input character you want to drop\n");
    scanf(" %c", &d);
    printf("Now character you want to substitute\n");
    scanf(" %c", &s);
    function2(my_string, d, s);
    printf("The string is %s\n", my_string);
    return EXIT_SUCCESS;
}

It works up until the point where you actually print the altered string. All I get is Segmentation fault (core dumped). Note that code for function was not mine (I found it on some website, so owner of the original code for function2- Thank you in advance). Any help would be appreciated!

chqrlie
  • 131,814
  • 10
  • 121
  • 189
Egor
  • 7
  • 4

3 Answers3

1

First of all, you should avoid using scanf. If you're interested for the reason and alternatives click here.

But back to your problem

while(str != NULL)

is an infinite loop, because the pointer won't become NULL

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

should do the trick. It checks each time if you've already arrived at the end of the string.

Community
  • 1
  • 1
TheGuy
  • 161
  • 1
  • 11
0
    if (str != null){
      while(str[i] != '\0'){
        if (str[i] == drop){
          str[i] = sub;
          num++;
        }
      i++;
      }
    }

str is a char array, with str != NULL, you check that the array point to a valid memory address. With while loop and i++, you loop all characters in array, because the string ends with '\0', you need to stop the loop with while(str[i] != '\0').

Stefano Balzarotti
  • 1,760
  • 1
  • 18
  • 35
0

Your function runs an infinite loop because str never become NULL, but since i is incremented, str[i] will eventually access memory beyond the end of the string and at some point invalid memory causing a Segmentation fault.

Note also that it is not simple to tell scanf() the maximum number of characters to read into my_string. Using fgets() is much safer and allows a whole phrase to be substituted.

Here is a corrected version:

#include <stdio.h>
#include <stdlib.h>

#define MAXLEN 100

int function2(char str[], char drop, char sub) {
    int num = 0;
    for (int i = 0; str[i] != '\0'; i++) {
        if (str[i] == drop) {
            str[i] = sub;
            num++;
        }
    }
    return num;
}

int main(void) {
    char d, s;
    char my_string[MAXLEN];

    printf("Input your string\n");
    if (!fgets(my_string, MAXLEN, stdin))
        return EXIT_FAILURE;
    printf("Input character you want to drop\n");
    if (scanf(" %c", &d) != 1)
        return EXIT_FAILURE;
    printf("Now character you want to substitute\n");
    if (scanf(" %c", &s) != 1)
        return EXIT_FAILURE;
    function2(my_string, d, s);
    printf("The modified string is %s", my_string);
    return EXIT_SUCCESS;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189