Dain1234 / Java-Programming

0 stars 0 forks source link

Find character in a String #9

Open Dain1234 opened 1 year ago

Dain1234 commented 1 year ago

package com.dain.logics;

import java.util.Scanner;

public class findcharinstring { public static void main(String[] args) { int x = 1; Scanner sc = new Scanner(System.in); System.out.println("Enter the string:"); String string = sc.nextLine(); System.out.println("Enter the character to find:"); char f = sc.next().charAt(0); int a = string.length(); char arr[] = new char[a]; for (int i = 0; i < a; i++) { arr[i] = string.charAt(i); if (arr[i] == f) { System.out.println("character " + f + " is found at " + i); x = 1; } } if (x == 0) { System.out.println("Character not found"); }

}

}