-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinterview_1.java
More file actions
59 lines (54 loc) · 2.1 KB
/
interview_1.java
File metadata and controls
59 lines (54 loc) · 2.1 KB
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
// Problem: Write a function that splits a text into single words.
public static void main (String [] args) {
String[] myresult;
System.out.println(“Please enter your text: “);
Scanner in = new Scanner(System.in);
String mystr = in.nextline(); // Read String into variable
myresult = mystr.split (“ +”); // this will ignore all the space
for (int i =0; i<myresult.length; i++)
System.out.println(myresult[i]);
}
// Problem: Write a function that splits a text into single words, without using split() or StringTokenizer etc that does the same job.
public static void main (String[] args) {
System.out.println(“Please enter your text: “);
Scanner in = new Scanner(System.in);
while (in.hasNext())
{
System.out.println(in.next());
}
}
// Problem: Write a function that splits a text into single words, without using split() or StringTokenizer etc that does the same job, and without using Scanner.next().
public static void main (String[] args) {
System.out.println (“Please enter your text: “);
Scanner in = new Scanner (System.in);
String mystr = in.nextln();
ArrayList<String> myarr = new ArrayList<String>();
for (int i=0; i<mystr.length; i++) {
String word = “”;
if(mystr[i] != “ “) {
word += mystr[i];
}
else { // seeing space
if (word != “”)
myarr.add(word);
word = “”;
}
}
if (word != “”)
myarr.add(word);
for (int =0; j<myarr.size(); j++)
System.out.println (myarr.get(i));
}
//Problem: Write a function that splits a text into single words, without using split() or StringTokenizer etc that does the same job, and without using Scanner.next(). Use indexOf() and/or substring() instead.
public static void main (String[] args) {
System.out.println (“Please enter your text: “);
Scanner in = new Scanner(System.in);
String mystr = in.nextln();
ArrayList<String> myarr = new ArrayList<String>();
while (mystr.indexOf (“ “) != -1)
if (mystr.indexOf(“ “) ! = 0)
myarr.add (mystr.substr(0, mystr.indexOf(“ “));
mystr = mystr.substr(mystr.indexOf(“ “)+1);
}
myarr.add(mystr);
}