English

Write a program for the following. Input a string and count the number of non alphabetic characters in it.

Advertisements
Advertisements

Question

Write a program for the following.

Input a string and count the number of non alphabetic characters in it.

Code Writing
Advertisements

Solution

import java.util.*;
class clTask
{
   void main()
   {
       String s1 = """;
       Scanner sc = new Scanner(System.in);
       char ch1, ch2;
       int count = 0;
       System.out.print(" Enter a sentence : ");
       s1 = sc.nextLine();
       for(int j = 0; j <= s1.length() -1; j++)
       {
           ch1 = s1.charAt(j);
           if(ch1 >= 'A' && ch1 <='Z' || ch1 >= 'a' && ch1 <='z')
           {
           }
           else
           {
              count++;
           }
       }
       System.out.println("Number of Non-alphabets is: "+ count);
   }
}

Output:

Enter a sentence : 27/F, MH Lane, Kol - 700 046

Number of Non-alphabets is : 19

shaalaa.com
  Is there an error in this question or solution?
Chapter 10: String Handling - Exercises [Page 248]

APPEARS IN

Rupa Pandit Computer Applications [English] Class 10 ICSE
Chapter 10 String Handling
Exercises | Q 4. (d) | Page 248
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×