7609
|
1 int main( int argc, char *argv[] ) {
|
|
2 int words = countwords(argv[1]);
|
|
3 char english[80], piglatin[80];
|
|
4 initialize(english, piglatin);
|
|
5 words = countwords(english);
|
|
6
|
|
7 /* Now Pig Latin Translator in C converts English to Pig Latin */
|
|
8 convert(words, english, piglatin);
|
|
9 writeoutput(piglatin);
|
|
10 }
|
|
11 int countwords(char english[])
|
|
12 {
|
|
13 int count, words = 1;
|
|
14 for (count = 0; count < 79; ++count)
|
|
15 if (english[count] == ' ' && english[count + 1] != ' ')
|
|
16 ++words;
|
|
17 return (words);
|
|
18 }
|
|
19 void initialize(char english[], char piglatin[])
|
|
20 {
|
|
21 int count;
|
|
22 for (count = 0; count < 80; ++count)
|
|
23 english[count] = piglatin[count] = ' ';
|
|
24 return;
|
|
25 }
|
|
26
|
|
27
|
|
28
|
|
29 /* now Pig Latin translator in C coverts each word into Pig Latin */
|
|
30 void convert(int words, char english[], char piglatin[])
|
|
31 {
|
|
32 int n, count;
|
|
33 int m1 = 0; /* m1 indicates the position of beginning of each word */
|
|
34 int m2; /* m2 indicates the end of the word */
|
|
35
|
|
36 /* convert each word */
|
|
37 for (n = 1; n <= words; ++n)
|
|
38 {
|
|
39 /* locating the end of the current word */
|
|
40 count = m1 ;
|
|
41 while (english[count] != ' ')
|
|
42 m2 = count++;
|
|
43
|
|
44 /* transposing the first letter of each word and adding 'a' at the end */
|
|
45 for (count = m1 ; count < m2; ++count)
|
|
46 piglatin[count + (n - 1)] = english[count + 1];
|
|
47 piglatin[m2 + (n - 1)] = english[m1];
|
|
48 piglatin[m2 + n] = 'a'; /* adding 'a' at the end */
|
|
49
|
|
50 /* reseting the initial marker */
|
|
51 m1 = m2 + 2;
|
|
52 }
|
|
53 return;
|
|
54 }
|
|
55
|
|
56
|
|
57 /* now Pig Latin translator in C displays the line of English text in Pig Latin */
|
|
58 void writeoutput(char piglatin[])
|
|
59 {
|
|
60 int count = 0;
|
|
61 for (count = 0; count < 80; ++count)
|
|
62 putchar(piglatin[count]);
|
|
63 printf("\n");
|
|
64 return;
|
|
65 } |