i have a problem in which i have to remove char from strings by sending one string and char ata time to a function.
in the below code it works fine for first string but for each successive string initial characters are not passed ..like for second call first chatcter of string will not be passed and so on.

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

#define MAXLEN 100
void fnRemChar(char*,char);
int main(int argc,char *argv[])
{

    system("clear");
    int iNoOfStr;
    char acInpStr[MAXLEN][MAXLEN];
    char cChar;
    printf("\nEnter number of strings to be entered(should not be greater than %d):",MAXLEN);
    fflush(stdin);
    scanf("%d",&iNoOfStr);
    void (*REMOVE) (char* ,char);

        /* REMOVE Pointer to Function fnRemChar */
        REMOVE = fnRemChar;

    for(int iCount=0;iCount<iNoOfStr;iCount++)
    {
        printf("\n ENTER %d String: ",iCount+1);
        getchar();
        fgets(acInpStr[iCount],MAXLEN,stdin);
        fflush(stdout);
        fflush(stdin);
    }
    printf("\n Enter character to be removed: ");
    scanf("%c",&cChar);

    for(int iCount=0;iCount<iNoOfStr;iCount++)
    {
        REMOVE(acInpStr[iCount],cChar);
    }
}
void fnRemChar(char *acInpStr,char cChar)
{
    char acFinalStr[MAXLEN];
    int iCount=0,iCount1=0;
    while(acInpStr[iCount]!='\0')
 if(acInpStr[iCount] != cChar)
        {
            printf("\n %c",acInpStr[iCount]);
            acFinalStr[iCount1]=acInpStr[iCount];
            iCount1++;
        }
        iCount++;
    }
    printf("\n %s\n",acFinalStr);
}

Recommended Answers

All 4 Replies

Enter number of strings to be entered(should not be greater than 100):3

ENTER 1 String: this

ENTER 2 String: that

ENTER 3 String: g

Enter character to be removed: t

his
ÿ8ð
ha

ÿ8ð

a

check out what I did here, I put three fprintf lines in here to check the values you are entering in your char**...Also this program had many errors...It won't compile for me so I changed some of it

Also why do you use a function pointer????

Note I quickly went through the code so I probably missed some things

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

#define MAXLEN 100
void fnRemChar(char*,char);
int main(int argc,char *argv[])
{
	int iCount=0;
    system("clear");
    int iNoOfStr;
    char acInpStr[MAXLEN][MAXLEN];
    char cChar;
    printf("\nEnter number of strings to be entered(should not be greater than %d):",MAXLEN);
    fflush(stdin);
    scanf("%d",&iNoOfStr);
    void (*REMOVE) (char* ,char);

        /* REMOVE Pointer to Function fnRemChar */
        REMOVE = fnRemChar;//function pointer????

	getchar();

    for(iCount;iCount<iNoOfStr;iCount++)
    {
        printf("\n ENTER %d String: ",iCount+1);
        fgets(acInpStr[iCount],MAXLEN,stdin);
        //fflush(stdout);
        //fflush(stdin);
    }
	fprintf(stdout, "s->%s\n", acInpStr[0]);//check first three
	fprintf(stdout, "s->%s\n", acInpStr[1]);//check first three
	fprintf(stdout, "s->%s\n", acInpStr[2]);//check first three
    printf("\n Enter character to be removed: ");
    scanf("%c",&cChar);

    for(iCount=0;iCount<iNoOfStr;iCount++)
    {
        REMOVE(acInpStr[iCount],cChar);
    }
}
void fnRemChar(char *acInpStr,char cChar)
{
    char acFinalStr[MAXLEN];
    int iCount=0,iCount1=0;
    while(acInpStr[iCount]!='\0')
	{ 
if(acInpStr[iCount] != cChar)
        {
            printf("\n %c",acInpStr[iCount]);
            acFinalStr[iCount1]=acInpStr[iCount];
            iCount1++;
        }
        iCount++;
    }
    printf("\n %s\n",acFinalStr);
}

this is getting compiled but output is not proper ..actually problem is with getting strings from user.

i did some changes and printed the array after getting values from user but strings are not getting pupulated in array properly .

void fnRemChar(char*,char);
int main(int argc,char *argv[])
{

    system("clear");
    int iNoOfStr;
    char acInpStr[MAXLEN][MAXLEN];
    char cChar;
    printf("\nEnter number of strings to be entered(should not be greater than %d):",MAXLEN);
    fflush(stdin);
    scanf("%d",&iNoOfStr);
    void (*REMOVE) (char* ,char);

        /* REMOVE Pointer to Function fnRemChar */
        REMOVE = fnRemChar;

    for(int iCount=0;iCount<iNoOfStr;iCount++)
    {
        printf("\n ENTER %d String: ",iCount+1);
        getchar();
        fgets(acInpStr[iCount],MAXLEN,stdin);
        fflush(stdout);
        fflush(stdin);
    }
    for(int iCount=0;iCount<iNoOfStr;iCount++)
    {
        printf("\n %s ",acInpStr[iCount]);
    }
    printf("\n Enter character to be removed: ");
    scanf("%c",&cChar);

    for(int iCount=0;iCount<iNoOfStr;iCount++)
    {
        REMOVE(acInpStr[iCount],cChar);
    }
}
void fnRemChar(char *acInpStr,char cChar)
{
    char acFinalStr[MAXLEN];
    int iCount=0,iCount1=0;
    while(acInpStr[iCount]!='\0')
    {
        if(acInpStr[iCount] != cChar)
        {
            acFinalStr[iCount1]=acInpStr[iCount];
            iCount1++;
            [B]acFinalStr[iCount1]='\0'[/B];
        }
        iCount++;
    }
    printf("\n %s",acFinalStr);
    fflush(stdin);
}

****** function pointer is used as it is specifically mentioned in problem statement to use function pointer****************


output:

Enter number of strings to be entered(should not be greater than 100):3

ENTER 1 String: this
ENTER 2 String: hthataaa
ENTER 3 String: g
//SCANNED VALUES IN ARRAY:
this

thataaa

Enter character to be removed: t

his
haaaa

Did you even look at what I posted?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.