0

I'm having trouble calling the function more than one time in my C program. The gist of the assignment is to replace the whitespaces in a sentence inputted from the user and replace it with a different character. For some reason, the program will call the same first function multiple times. I tried putting the strlen(x) in a variable inside my function, but I'm not very well versed in the C language, so I decided to leave it out of my code.

#include <string.h>

void display(char x[], char y);

void main(){
    //Do not change this function
    char a[100];
    printf("Enter a sentence\n");
    gets(a);
    display(a, '*');        //To replace every space by *
    display(a, '-');        //To replace every space by -
    display(a, '+');        //To replace every space by +
}

void display(char x[], char y){
    for(char i = 0; i < strlen(x); i++) {
        if(x[i] == ' ') {
            x[i] = y;
        }
    }
    printf("%s\n", x);
}

enter image description here

Heronxis
  • 11
  • 3
  • Welcome to SO! [Please don't post images of code/output](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-on-so-when-asking-a-question). Instead of overwriting the spaces in your loop, just print each character directly, making the substitution as needed. Hopefully the next lesson is how to run a [buffer overflow attack](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) on this application. – ggorlen Dec 09 '19 at 03:02
  • 2
    Copy the original string in a buffer before you call `display()`, like `char buf[100]; ... strcpy(buf, a); display(buf, '*');strcpy(buf, a); display(buf, '-');...` – Déjà vu Dec 09 '19 at 03:16
  • Use [`fgets`](https://linux.die.net/man/3/fgets) instead of `gets`, which is a huge security vulnerability – yano Dec 09 '19 at 03:48
  • the posted code does not compile! amongst other things, the statement: `#include ` is missing (and needed for the calls to `printf()` – user3629249 Dec 10 '19 at 03:22
  • regarding: `gets(a);` The function: `gets()` has been depreciated for many years and (recently) completely removed from the C standard. Strongly suggest using: `fgets()` (which has a different parameter list, see the MAN page for details – user3629249 Dec 10 '19 at 03:23
  • regarding: `//Do not change this function .... gets(a);` If your instructor presented this function, then I suggest getting another instructor – user3629249 Dec 10 '19 at 03:25

1 Answers1

3

It does not "call the same first function". You change the value of your string inside the function, so after the first run of the function the string does not have spaces. Therefore the second and third call print the string unchanged:

void display(char x[], char y){
    for(char i = 0; i < strlen(x); i++) {
        if(x[i] == ' ') {
           // this happens only upon first call! 
           x[i] = y;
       }
    }
    printf("%s\n", x);
}

Edit: to fix the issue, for example see the comment Ring Ø added and follow the advice

Anatoliy R
  • 1,749
  • 2
  • 14
  • 20