-
Notifications
You must be signed in to change notification settings - Fork 617
/
Copy pathpiglatin_convertor.java
94 lines (75 loc) · 1.93 KB
/
piglatin_convertor.java
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
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* In Pig Latin -
* - words starting with consonant - take the consonant cluster and
* move it to the end of the word, adding the suffix - 'ay'.
* - words starting with vowel - add the suffix 'hey' to the word
*/
import java.util.*;
public class piglatin_convertor
{
public void main()
{
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string: ");
String str = scanner.nextLine();
System.out.println("In piglatin: ");
System.out.println(string_piglatin(str));
}
String string_piglatin(String str)
{
str = str + " ";
String w = "", piglatin = "";
char ch;
int i;
for(i = 0; i < str.length(); i++)
{
ch = str.charAt(i);
if(ch == ' ')
{
piglatin += word_piglatin(w) + ' ';
w = "";
}
else
{
w += ch;
}
}
return piglatin;
}
String word_piglatin(String w)
{
int i;
char ch, ch1 = w.charAt(0);
w.toLowerCase();
if(ch1 == 'a' || ch1 == 'e' || ch1 == 'i' || ch1 == 'o' || ch1 == 'u')
{
return w+"hay";
}
for(i = 0; i < w.length(); i++)
{
ch = w.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
break;
}
return ( w.substring(i) + w.substring(0,i) + "ay");
}
}
/*
* Test Cases-
*
* 1.
* Enter a string:
* how are you
* In piglatin:
* owhay arehay ouyay
*
* 2.
* Enter a string:
* i am good
* In piglatin:
* ihay amhay oodgay
*
* Time Complexity: O(n)
* Space Complexity: O(n)
* where n is the number of characters in the string
*/