EmbLogic's Blog

how to take string as input from the user ???

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

int main()
{
int d;
printf(“enter size of the string\n”);
scanf(“%d”,&d);
char a[100];
char *p;
p = malloc(sizeof(char)*d);

printf(“enter the string\n”);
fflush(stdin);
fgets(p,d,stdin);                             //didn’t worked with gets(p)?????????
int i,j,count=0,flag=0;
for(i=0;i<=”;i++)
{
for(j=0;j<i;j++)
{
if(*(p+i)==a[j])
{
flag=-1;
}
}
if(flag==0)
{
a[count++]=*(p+i);
}
flag=0;
}
printf(“modified array is \n”);
for(j=0;j<”;j++)
{
printf(“%c”,a[j]);
}
}

 

One Response to how to take string as input from the user ???

  1. m siddarth says:

    Implement the following code before any scanf() or fgets().

    int c;
    /* discard all characters up to and including newline */
    while ((c = getchar()) != ‘\n’ && c != EOF);
    Reason: it’s because of the \n stuck in the input stream and we have to flush it out every time.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>