-
Notifications
You must be signed in to change notification settings - Fork 124
/
exercise06.c
81 lines (60 loc) · 1.47 KB
/
exercise06.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// C Primer Plus
// Chapter 11 Exercise 6:
// Write a function called is_within() that takes a character and a string
// pointer as its two function parameters. Have the function return a
// nonzero value (true) if the character is in the string and zero (false)
// otherwise. Test the function in a complete program that uses a loop to
// provide input values for feeding to the function.
#include <stdio.h>
#define LIMIT 50
int is_within(char ch, const char *string);
char * get(char *string, int n);
int main(void)
{
// test is_within()
char string[LIMIT];
char ch;
printf("Enter a string to search: ");
get(string, LIMIT);
while (string[0] != '\0')
{
printf("Enter a character to search for: ");
ch = getchar();
if (ch != '\n')
while (getchar() != '\n')
continue;
char *contains = is_within(ch, string) ? "" : " not";
printf("\"%s\" does%s contain %c\n", string, contains, ch);
printf("Enter a string to search (empty line to quit): ");
get(string, LIMIT);
}
puts("Bye");
return 0;
}
int is_within(char ch, const char *string)
{
// returns 1 if char ch is in string
// returns 0 otherwise
while(*string != '\0')
{
if (*string == ch)
return 1;
string++;
}
return 0;
}
char * get(char *string, int n)
{
// wrapper for fgets that replaces first newline with null
char *return_value = fgets(string, n, stdin);
while (*string != '\0')
{
if (*string == '\n')
{
*string = '\0';
break;
}
string++;
}
return return_value;
}