-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.c
72 lines (61 loc) · 1.5 KB
/
cat.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
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"cs50.h"
int indexOfWord(string S, string word ){
int i=1,nb=1;
string w = strtok(S," ");
if (strcmp(w,word) == 0) return 1;
i++;
nb++;
while((w = strtok(NULL," "))&&(nb<=200))
{
if (strcmp(w,word)==0)
return i;
i++; nb++;
}
return -1;
}
int main (void)
{
string S = GetString();
if (S == NULL)
{ printf("There is no box"); return 0;}
char cat[] = "cat";
int i = indexOfWord(S, cat);
char ordinal[2];
if (i == -1)
printf("No cat yet");
else
{ switch (i%10)
{
case 1:
{
if (i/10%10 != 1) // not *11
{ordinal[0] = 's'; ordinal[1] = 't'; ordinal[2] = '\0';}
else
{ordinal[0] = 't'; ordinal[1] = 'h'; ordinal[2] = '\0';}
break;
}
case 2:
{
if (i/10%10 != 1) // not *11
{ordinal[0] = 'n'; ordinal[1] = 'd'; ordinal[2] = '\0';}
else
{ordinal[0] = 't'; ordinal[1] = 'h'; ordinal[2] = '\0';}
break;
}
case 3:
{
if (i/10%10 != 1) // not *11
{ordinal[0] = 'r'; ordinal[1] = 'd'; ordinal[2] = '\0';}
else
{ordinal[0] = 't'; ordinal[1] = 'h'; ordinal[2] = '\0';}
break;
}
default: ordinal[0] = 't'; ordinal[1] = 'h'; ordinal[2] = '\0';
}
printf("The cat is the %d%s item in the box", i, ordinal);
}
return 0;
}