First X Consonants

The program must accept a string S and an integer X as the input. The program must print the first X consonants in the string S as the output. If the number of consonants in the string S is less than X then the program must print -1 as the output.
Note: The string S contains only alphabets.

Boundary Condition(s):
1 <= Length of S <= 100
1 <= X <= Length of S

Input Format:
The first line contains the string S.
The second line contains the value of X.

Output Format:
The first line contains either the first X consonants in S or -1.

Example Input/Output 1:
Input:
Letuscrack
5

Output:
Ltscr

Explanation:
The first 5 consonants in “Letuscrack” are L, t, s, c, and r.
So they are printed as the output.

Example Input/Output 2:
Input:
Dengue
6

Output:
-1

#include<stdio.h>
#include <stdlib.h>

int isvowel(char ch)
{
    ch=tolower(ch);

    if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
    {
        return 1;
    }

    return 0;
}
int main()
{
    char str[100];
    int x,z=0;

    scanf("%sn%d",str,&x);

    int len=strlen(str);
    char arr[len];

    for(int i=0;i<len;i++)
    {
        if(!isvowel(str[i]))
        {
            arr[z]=str[i];
            z++;
        }

    }

    if(z<x)
    printf("-1");
    else
    {
        for(int i=0;i<x;i++)
        {
            printf("%c",arr[i]);
        }
    }

}
def is_vowel(ch):
    return ch in 'AEIOUaeiou'

s = input().strip()
x = int(input())

consonants = [ch for ch in s if not is_vowel(ch)]

if len(consonants) < x:
    print(-1)
else:
    print(''.join(consonants[:x]))
#include <iostream>
#include <string>
using namespace std;

bool is_vowel(char ch) {
    return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
           ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}

int main() {
    string s;
    int x;
    cin >> s >> x;

    string consonants = "";
    for(char ch : s) {
        if(!is_vowel(ch)) {
            consonants += ch;
        }
    }

    if(consonants.length() < x) {
        cout << -1;
    } else {
        cout << consonants.substr(0, x);
    }
    return 0;
}
import java.util.Scanner;

public class FirstXConsonants {
    public static boolean isVowel(char ch) {
        return ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U' ||
               ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        int x = sc.nextInt();

        StringBuilder consonants = new StringBuilder();
        for(char ch : s.toCharArray()) {
            if(!isVowel(ch)) {
                consonants.append(ch);
            }
        }

        if(consonants.length() < x) {
            System.out.println(-1);
        } else {
            System.out.println(consonants.substring(0, x));
        }
    }
}
Previous Article

Check for Increasing Order of Digits

Next Article

Count Digits That Are Fibonacci Numbers

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *