Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

I cant figure these errors out and i've searched everywhere, I need help.

–1 vote
asked Dec 12, 2019 by MY man FS
#include <stdio.h>

#include <string.h>

#define SIZE 20

void pluralize(char str1[]);

void removal(char *p, char c);

char str1[SIZE];

int main(){

printf("This finds the pluralized version of chair, dariy, circus, fly, dog, church, clue, or dish :\n");

    char str1[SIZE];

    sscanf("%s", str1);

    

    while (str1[0] != '9'){

pluralize(str1);

        return 0;

    }

}

void pluralize(char str1[]){

int i;

 char last_char = str1[strlen(str1)-1];

if( last_char == 'y'){

removal(str1,'y');

strcat(str1,"ies");

printf("%s", str1);

    

}

if else(last_char == 'h'){

    strcat(str1,"es");

    printf("%s", str1);

}

if else(last_char == 's'){

        strcat(str1,"es");

        printf("%s", str1);

}

if else(last_char == 'r'){

        strcat(str1,"s");

        printf("%s", str1);

}

if else(last_char == 'r'){

        strcat(str1,"s");

        printf("%s", str1);

}

if else(last_char == 'g'){

        strcat(str1,"s");

        printf("%s", str1);

}

if else(last_char == 'e'){

        strcat(str1,"s");

        printf("%s", str1);

}

}

void removal(char *p, char c){

    char *pdest = p;

    

    while (*p){

        

    if (*p != c){

        

        *pdest++ = *p;

        p++;

    }

    *pdest='\0';

    

    }

    

}

3 Answers

0 votes
answered Dec 12, 2019 by anonymous
#include <stdio.h>

#include <string.h>

#define SIZE 20

void pluralize(char str1[]);

void removal(char *p, char c);

char str1[SIZE];

int main(){

printf("This finds the pluralized version of chair, dariy, circus, fly, dog, church, clue, or dish :\n");

    char str1[SIZE] ;

    scanf("%s", str1);

    

    while (str1[0] != '9'){

pluralize(str1);

        return 0;

    }

}

void pluralize(char str1[]){

int i;

 char last_char = str1[strlen(str1)-1];

if( last_char == 'y'){

removal(str1,'y');

strcat(str1,"ies");

printf("%s", str1);

    

}

 else if(last_char == 'h'){

    strcat(str1,"es");

    printf("%s", str1);

}

else if(last_char == 's'){

        strcat(str1,"es");

        printf("%s", str1);

}

else if(last_char == 'r'){

        strcat(str1,"s");

        printf("%s", str1);

}

else if(last_char == 'r'){

        strcat(str1,"s");

        printf("%s", str1);

}

else if(last_char == 'g'){

        strcat(str1,"s");

        printf("%s", str1);

}

else if(last_char == 'e'){

        strcat(str1,"s");

        printf("%s", str1);

}

}

void removal(char *p, char c){

    char *pdest = p;

    

    while (*p){

        

    if (*p != c){

        

        *pdest++ = *p;

        p++;

    }

    *pdest='\0';

    

    }

    

}
0 votes
answered Dec 13, 2019 by gameforcer (2,950 points)

1.

sscanf doesn't work for me here on onlinegdb, so you might've had the same problem.

2.

Your while loop in main is pointless, also you have

if else()

{

}

instead of

else if()

{

3.

Your "else if"s have repeating cases for the same letters. What's more the entire construction could be done with a single switch and look more clear.

4.

Your removal function logic is wrong and I'm too lazy to write about why. Instead I will show you correct solution which you aimed to have, as well as the rest of revised and working code.

Link: https://onlinegdb.com/H1i1x1-RB

0 votes
answered Dec 17, 2019 by vetri
ERRORS ARE CLEARED.

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

#define SIZE 20

void pluralize(char str1[]);
void removal(char *p, char c);

char str1[SIZE];

int main()
{
    printf("This finds the pluralized version of chair, dariy, circus, fly, dog, church, clue, or dish :\n");
    char str1[SIZE];
    scanf("%s", str1);

    while (str1[0] != '9')   //check this logic. this is wrong.
    {
        pluralize(str1);
    }

    return 0;
}

void pluralize(char str1[])
{
    int i;
    char last_char = str1[strlen(str1)-1];
    
    if( last_char == 'y')
    {
        removal(str1,'y');
        strcat(str1,"ies");
        printf("%s", str1);
    }

    else if(last_char == 'h')
    {
    strcat(str1,"es");
    printf("%s", str1);
    }

    else if (last_char == 's')
    {
        strcat(str1,"es");
        printf("%s", str1);
    }

    else if (last_char == 'r')
    {
        strcat(str1,"s");
        printf("%s", str1);
    }

    else if(last_char == 'r')
    {
        strcat(str1,"s");
        printf("%s", str1);
    }

    else if(last_char == 'g')
    {
        strcat(str1,"s");
        printf("%s", str1);
    }

    else if(last_char == 'e')
    {
        strcat(str1,"s");
        printf("%s", str1);
    } else {
        ;
    }
}

void removal(char *p, char c)
{
    char *pdest = p;
    
    //while (*p) // removed this while loop or else it will get stuck here
    {
        if (*p != c){
            *pdest++ = *p;
            p++;
        }
        *pdest='\0';
    }
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...