xref: /illumos-gate/usr/src/cmd/sgs/tools/common/piglatin.c (revision 45ede40b2394db7967e59f19288fae9b62efd4aa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  *	Copyright 2007 Sun Microsystems, Inc.
23  *	All rights reserved.
24  *	Use is subject to license terms.
25  *
26  * Very crude pig latin converter.
27  * Piglatin is an encoded form of English that is often used by  children
28  * as a game. A piglatin word is formed from an English word by:
29  *
30  *  .	If the word begins with a consonant, move the consonant to the end of
31  *	the word and append the letters "ay". Example: "door" becomes "oorday".
32  *
33  *  .	If the word begins with a vowel, merely append "way" to the word.
34  *	Example: "ate" becomes "ateway
35  */
36 #pragma ident	"%Z%%M%	%I%	%E% SMI"
37 
38 #include <stdio.h>
39 #include <strings.h>
40 #include <ctype.h>
41 
42 int
43 main()
44 {
45 	char	buffer[32767], * cb, * sb;
46 	int	ic, ignore = 0, word = 0;
47 
48 	sb = cb = &buffer[0];
49 	*sb = '\0';
50 
51 	while ((ic = getc(stdin)) != EOF) {
52 		char c = (char)ic;
53 
54 		/*
55 		 * Ignore all possible formatting statements.
56 		 */
57 		if (c == '%')
58 			ignore = 1;
59 
60 		/*
61 		 * Isolate the word that will be converted.
62 		 */
63 		if (isspace(ic) || (ispunct(ic) && ((ignore == 0) ||
64 		    ((c != '%') && (c != '.') && (c != '#') && (c != '-'))))) {
65 			char s = buffer[0];
66 
67 			/*
68 			 * If we haven't collected any words yet simply
69 			 * printf the last character.
70 			 */
71 			if (word == 0) {
72 				(void) putc(ic, stdout);
73 				continue;
74 			}
75 
76 			/*
77 			 * Leave format strings alone - contain "%".
78 			 * Leave single characters alone, typically
79 			 * these result from "\n", "\t" etc.
80 			 */
81 			if ((ignore == 0) && ((cb - buffer) > 1)) {
82 				if ((s == 'a') || (s == 'A') ||
83 				    (s == 'e') || (s == 'E') ||
84 				    (s == 'i') || (s == 'I') ||
85 				    (s == 'o') || (s == 'O') ||
86 				    (s == 'u') || (s == 'U')) {
87 					/*
88 					 * Append "way" to the word.
89 					 */
90 					(void) strcpy(cb, "way");
91 				} else {
92 					/*
93 					 * Move first letter to the end
94 					 * of the word and add "ay".
95 					 */
96 					sb++;
97 					*cb = s;
98 					(void) strcpy(++cb, "ay");
99 				}
100 			} else
101 				*cb = '\0';
102 
103 			/*
104 			 * Output the collected buffer, the last character
105 			 * read and reinitialize pointers for next round.
106 			 */
107 			(void) printf("%s", sb);
108 			(void) putc(ic, stdout);
109 
110 			sb = cb = &buffer[0];
111 			word = ignore = 0;
112 
113 			continue;
114 		}
115 
116 		/*
117 		 * Store this character into the word buffer.
118 		 */
119 		*cb++ = c;
120 		*cb = '\0';
121 		word = 1;
122 	}
123 
124 	return (0);
125 }
126