17. Berilgan matndagi so’zlarni ulardagi simvollar soni bo’yicha kamayish va o’sish tartibida chiqaruvchi dastur tuzing


Download 125.14 Kb.
bet1/2
Sana04.12.2020
Hajmi125.14 Kb.
#158660
  1   2
Bog'liq
Eden doc


Tohirov Sarvar

Java 3-labaratoriya ishi



17. Berilgan matndagi so’zlarni ulardagi simvollar soni bo’yicha kamayish va o’sish tartibida chiqaruvchi dastur tuzing.

package com.company;

import java.util.*;
import java.util.Scanner;

public class matn2 {


public static void main(String[] args) {
Scanner ha = new Scanner(System.in);
System.out.println("s'zlarni kiriting");
String s = ha.nextLine();
String[] a = s.split(" ");
for (int i = 0; i < s.length(); i++) {

}
System.out.println("Kamayish tartibidagi holati");


sort(a);
printArray(a);
}

static void sort(String[] a) {


for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {
if (a[i].length() < a[j].length()) {
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
}

static void printArray(String[] a) {


for (int i = 0; i < a.length; i++) {
System.out.printf(a[i] + " ");
}
System.out.println();
}
}

O’sish tartibida

package com.company;

import java.util.Scanner;

public class matn_2_1 { public static void main(String[] args) {
Scanner ha = new Scanner(System.in);
System.out.println("s'zlarni kiriting");
String s = ha.nextLine();
String[] a = s.split(" ");

System.out.println("Kamayish tartibidagi holati");


sort(a);
printArray(a);
}
static void sort(String[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = i + 1; j < a.length; j++) {

if (a[i].length() < a[j].length()) {


String temp = a[i];
a[i] = a[j];
a[j] = temp;
} } } }

static void printArray(String[] a) {


for (int i = a.length; i >=1 ; i--) {
System.out.printf(a[i-1]+ " ");
}
System.out.println();
}
}

2. Berilgan so’z satrda necha marta uchrashini aniqlang.


Download 125.14 Kb.

Do'stlaringiz bilan baham:
  1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling