Вы находитесь на странице: 1из 3

/*

1. Прочитать строку из пользовательского ввода и


вернуть на консоль кол-во повторений символа 'n' в этой строке.
2. Прочитать строку из пользовательского ввода и сохранить
в массив все позиции символа 'n' в этой строке.
abcaabca, a -> [0,3,4,7]
3. Написать программу, которая читает два числа из
пользовательского ввода и возводит первое число в степень второго числа.
4. Прочитать строку из пользовательского ввода и вернуть пользователю
статистику о том, сколько раз встречался каждый из символов
(пользователь может вводить только алфавитные латинские символы, lowercase)
*/

import java.util.Arrays;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {

Scanner sc = new Scanner(System.in);


String command = null;
while(true) {
System.out.println("Type 1-4 to begin or type \"exit\" or \"quit\" ");
System.out.println("""
What task should be performed?
1. Прочитать строку из пользовательского ввода и вернуть на консоль
кол-во повторений символа 'n' в этой строке.
2. Прочитать строку из пользовательского ввода и сохранить в массив
все позиции символа 'n' в этой строке
3. Написать программу, которая читает два числа из
пользовательского ввода и возводит первое число в степень второго числа
4. Прочитать строку из пользовательского ввода и вернуть
пользователю статистику о том, сколько раз встречался каждый из символов
(пользователь может вводить только алфавитные латинские символы, lowercase)
Type here: """);
command = sc.nextLine();
switch (command) {
case "1":
System.out.print("Type a string: ");
String stringForCount = sc.nextLine();
int nRepeat = countDuplicate(stringForCount, 'n');
System.out.println("\'n\' in string \"" + stringForCount + "\" has "
+ nRepeat + " repeatations");
break;

case "2":
System.out.print("Type another string: ");
String stringForPosition = sc.nextLine();
int[] position = storePosition(stringForPosition, 'n');
System.out.println("\'n\' in string \"" + stringForPosition +
"\" has " + Arrays.toString(position) + " positions");
break;

case "3":
try {
System.out.print("Type first number: ");
int first = Integer.parseInt(sc.nextLine());
System.out.print("Type second number: ");
int second = Integer.parseInt(sc.nextLine());
long pow = myPow(first, second);
System.out.println(first + " in power " + second + " = " +
pow);
}
catch(NumberFormatException e){
System.out.println(e.getMessage() + " is wrong number format");
}
break;

case "4":
System.out.println("Type a string for statistics: ");
String statistics = sc.nextLine();
printStatistic(statistics);
break;

case "exit":
case "quit":
sc.close();
return;
default:
System.out.println("Wrong command");
break;
}
}
}

public static int countDuplicate(String str, char ch){


int count = 0;
for(int i = 0; i < str.length(); ++i){
if(str.charAt(i) == ch){
++count;
}
}
return count;
}

public static int[] storePosition(String str, char ch){


int[] positionSet = new int[countDuplicate(str, ch)];
for(int i = 0, k = 0; i < str.length(); ++i){
if(str.charAt(i) == ch){
positionSet[k] = i;
++k;
}
}
return positionSet;
}

public static long myPow(int number, int power){


if (power < 0){
return -1;
}
if (power == 0){
return 1; //any number in power 0 is 1
}
if (power == 1){
return number; //any number in power 1 is the same number
}
long result = number;
for(int i = 2; i <= power; ++i){
result *= number;
}
return result;
}

private static int[] countPosition(String str){


int[] alphabet = new int[26]; // 26 letters in latin
for (int i = 0; i < alphabet.length; ++i){
alphabet[i] = countDuplicate(str, (char)(i + 97)); // 'a' has ASCII
code 97
}
return alphabet;
}

public static void printStatistic(String str){


int[] alphaStatistic = countPosition(str);
for(int i = 0; i < alphaStatistic.length; ++i){
if (alphaStatistic[i] > 0){
System.out.println("Letter \'" + (char)(i + 97) + "\' has " +
alphaStatistic[i] +
" occurenses");
}
}
}
}

Вам также может понравиться