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