N
Nezhate
Hi all,
I've not written c code for many times a go, and now I can't
understand why this occur when executing the next program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char line[100];
char s;
int begins(char string1[], char string2)
{
char answer[6];
if (string1[0]==string2)
{
strcpy(answer,"True");
printf ("%s, %s begins with %c\n",answer,string1,string2);
}
else
{
strcpy(answer,"False");
printf("%s, %s do not begin with %c\n",answer,string1,string2);
}
return(0);
}
int main ()
{
// program to test begins function
while(1)
{
printf ("Enter a new line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0'; // To trim :remove the character
'\0'added by fgets
printf("Enter the character you wish!\n");
scanf("%c",&s);
begins(line,s);
}
exit(0);
}
When executing first time all is good but in the second time I can't
enter a new line
$ ./exo_9_2
Enter a new line:
This line begins with T
Enter the character you wish!
T
True, This line begins with T begins with T
Enter a new line:
Enter the character you wish!
I've not written c code for many times a go, and now I can't
understand why this occur when executing the next program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char line[100];
char s;
int begins(char string1[], char string2)
{
char answer[6];
if (string1[0]==string2)
{
strcpy(answer,"True");
printf ("%s, %s begins with %c\n",answer,string1,string2);
}
else
{
strcpy(answer,"False");
printf("%s, %s do not begin with %c\n",answer,string1,string2);
}
return(0);
}
int main ()
{
// program to test begins function
while(1)
{
printf ("Enter a new line:\n");
fgets(line,sizeof(line),stdin);
line[strlen(line)-1]='\0'; // To trim :remove the character
'\0'added by fgets
printf("Enter the character you wish!\n");
scanf("%c",&s);
begins(line,s);
}
exit(0);
}
When executing first time all is good but in the second time I can't
enter a new line
$ ./exo_9_2
Enter a new line:
This line begins with T
Enter the character you wish!
T
True, This line begins with T begins with T
Enter a new line:
Enter the character you wish!