Showing posts with label Strings. Show all posts
Showing posts with label Strings. Show all posts

Friday, 14 January 2022

Random password generator in Java

     Random password generator in Java

Source code:


mport java.io.*;

import java.util.*;



public class Main

{

public static void main(String[] args) {

    Scanner in=new Scanner(System.in);

    

    System.out.println("How many pw do you need");

    

    int total=in.nextInt();

    

    System.out.println("How long");

    int len=in.nextInt();

    

    // '0'-'9' --> 48 - 57

    // 'A' - 'Z' --> 65-90

    // 'a' - 'z' --> 97-122

    

    ArrayList<String> listOfPasswords=new ArrayList<>();

    

    // for looping total number of passwords

    for(int i=0;i<total;i++){

        // generate one random password

        

        String randomPassword="";

        for(int j=0;j<len;j++){

            //generate one random character

            randomPassword=randomPassword+randomCharacter();

            

        }

     

        listOfPasswords.add(randomPassword);

    }

  for(String i: listOfPasswords)  

System.out.println(i+"");

}

public static char randomCharacter(){

    

    // generate a random number represents all possible character 

    // in out psword

    

    // 10 digits+26 uppercase+ 26 lower case = 62 possible values

    

    int rand=(int)(Math.random()*62);

    

    if(rand<=9){

        

        rand=rand + 48;

        return (char)(rand);

    }else if(rand<=35){

        rand = rand + 55;

        return (char)(rand);

    }else{

        rand = rand + 61;

        return (char)(rand);

    }

  

}

}


Sunday, 18 July 2021

Palindrome Check using Recursion

 Problem: 

You are given a String 'S'. You need to recursively find the if the string is Palindrome or not.

Test case 1: 

Input: abba

Output: True

Test case 2: 

Input: abbcbba

Output: True

Test case 3: 

Input: abbad

Output: False

Source code:


import java.util.*;

import java.io.*;



public class Main

{

    static int k=1;

public static void main(String[] args) 

{

    Scanner s=new Scanner(System.in);

    System.out.println("Enter String");

    String str =s.next();

System.out.println("Answer: "+isPalindrome(str,0,str.length()-1));

}

public static boolean isPalindrome(String str,int start,int end)

{

    if(start>=end)

        return true;

        

    return ((str.charAt(start)==str.charAt(end)) && isPalindrome(str,start+1,end-1));

}

}





 

Tuesday, 31 July 2018

Question: 

To find the number of vowels present in a sentence.

Test Case:

Solution:

#include <stdio.h>
#include <conio.h>
int main()
{
     int cha,i,c=0;
     char ch,str[40]="";
     printf("enter string : ");
     scanf("\n %s",str);
     for(i=0;str[i]!='\0';i++)
     {
     cha = str[i];
     switch(cha){
          case'a' :
          case'A' :
          case'e' :
          case'E' :
          case'i' :
          case'I' :
          case'o' :
          case'O' :
          case'u' :
          case'U' :
          c++;
     }
     }

     printf("\nTotal number of vowels is %d",c);
return 0;
}
Number Challenge
Mike set off with great zeal to the "Kracker Jack Fun Fair 2017". There were numerous activities in the fair, though Mike being a math expert, liked to participate in the Number Challenge.

Mike was given a string D of numbers containing only digits 0's and 1's. His challenge was to make the number to have all the digits same. For that, he should change exactly one digit, i.e. from 0 to 1 or from 1 to 0. If it is possible to make all digits equal (either all 0's or all 1's) by flipping exactly 1 digit then he has to output "Yes", else print "No" (without quotes).

Write a program to help Mike win over his challenge.


Input Format:
First and only input contains a string D of numbers made of only digits 1's and 0's.

Output Format:
Output “Yes" or a "No", depending on whether its possible to make it all 0s or 1s or not.
Refer sample input and output for formatting specifications.


Sample Input1:
101
Sample Output1:
Yes

Sample Input2:
11
Sample Output2:
No


Solution:
#include<stdio.h>
#include<string.h>
int main()
{
    int sum=0,i,l;
    char d[50];
    scanf("%s",d);
    l=strlen(d);
    for(i=0;i<l;i++)
    if(d[i]=='1')
    sum=sum+1;
    if(sum==1||(sum==(l-1)))
    printf("Yes");
    else
    printf("No");
    return 0;
} 
Alternating Code
It is IPL Season and the first league match of Dhilip’s favorite team, "Chennai Super Kings". The CSK team is playing at the IPL after 2 years and like all Dhoni lovers, Dhilip is also eagerly awaiting to see Dhoni back in action.

After waiting in long queues, Dhilip succeeded in getting the tickets for the big match. On the ticket, there is a letter-code that can be represented as a string of upper-case Latin letters.

Dhilip believes that the CSK Team will win the match in case exactly two different letters in the code alternate. Otherwise, he believes that the team might lose. Please see note section for formal definition of alternating code.

You are given a ticket code. Please determine, whether CSK Team will win the match or not based on Dhilip’sconviction. Print "YES" or "NO" (without quotes) corresponding to the situation.

Note:
Two letters x, y where x != y are said to be alternating in a code, if code is of form "xyxyxy...".


Input Format:
First and only line of the input contains a string S denoting the letter code on the ticket.

Output Format:
Output a single line containing "Yes" (without quotes) based on the conditions given and "No" otherwise.
Refer sample input and output for formatting specifications.


Sample Input1:
ABABAB
Sample Output1:
Yes

Sample Input2:
ABC
Sample Output2:
No


Solution:
 #include<stdio.h>
#include<string.h>
int main()
{
    int i;
    char a[50];
    scanf("%s",a);
    for(i=0;(a[i]!='\0');i++)
    {
        if(a[i]!=a[i+2]||a[i+2]=='\0'||a[i]==a[i+1])
        break;
    }
    if((a[i+2]=='\0'))
    printf("Yes");
    else
    printf("No");
    return 0;
 }

Random password generator in Java

       Random password generator in Java Source code: mport java.io.*; import java.util.*; public class Main { public static void main(Str...