|
CS256 - Fundamentals of Structured Programming
| Displaying exercises/e7/solution/p3.c
#include <stdio.h>
#define K 1024
/**
* Make a program that reads a line of input (up to 1024 characters, using
* fgets,) and scans the input for the lowercase letter 'x'. If it finds an x,
* it should print "The string has an x in it.\n" and exit, if it does not, it
* should print "The string did not have an x in it.\n", then exit. You may
* not use any standard library functions other than fgets for this assignment.
*/
int main(void)
{
char buf[K+1];
fgets(buf, K, stdin);
for(int i=0; buf[i] != '\0'; i++) {
if (buf[i] == 'x') {
printf("The string has an x in it.\n");
return 0;
}
}
printf("The string did not have an x in it.\n");
return 0;
}
|