-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChallenge 2
36 lines (36 loc) · 1.13 KB
/
Challenge 2
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
package project1;
import java.util.Scanner;
import java.util.ArrayList;
public class Multipleletters
{
public static void main(String[] args)
{
String word;
int i=0,j=0,z=1,length;
Scanner sc=new Scanner(System.in);
word=sc.nextLine(); // taking the input string from the user.
char[] newword=word.toLowerCase().toCharArray(); // converting the input string to character array by first making all the alphabets as lowercase.
length=word.length(); // calculating length of string.
if(length==1)
{
System.out.println(newword[0]+"1");
}
for(i=0;i<length-1;i++)
{
if(newword[j]==newword[j+1])
{
z++;
}
else
{
System.out.print(""+newword[j]+""+z); // z is the number of times each consecutive alphabet repeats.
z=1;
}
j++;
if(j==(length-1))
{
System.out.print(""+newword[j]+""+z); // condition where the pointer reaches to the last letter.
}
}
}
}