मराठी

Write a program by using class with the following specifications: Class name: Characters Data members/Instance variables str: String Member Methods: void input (String st): to assign st to str - Computer Applications

Advertisements
Advertisements

प्रश्न

Write a program by using class with the following specifications:

Class name: Characters
Data members/Instance variables
str: String
Member Methods:
void input (String st): to assign st to str
void check_print(): to check and print the following:

  1. number of letters
  2. number of digits
  3. number of uppercase characters
  4. number of lowercase characters
  5. number of special characters
कोड लेखन

उत्तर

import java.util*;
public class Characters
{String str
void input(String st)
{ str = st;
}

void check_print()
{char c;
int up = 0, low = 0, digi = 0, s = 0;
for (int i = 0; i < str.length(); i++)
{c = str.charAt(i);
if (c >= 97 && c <= 122) // ascii range of lowercase
low++;
else if (c >= 65 && c <= 97) // ascii range of uppercase
up++; 
else if (c >= 48 && c <= 57) // ascii range of digits
digi++;
else
s++;
}
System.out.println("Letters: " + (low + up));
System.out.println("Digits: " + digi);
System.out.println("Upper Case: " + up);
System.out.println("Lower Case: " + low);
System.out.println("Special: " + s);
}

public static void main(String args[])
{Characters obj = new Characters();
Scanner ob = new Scanner(System.in);
System.out.println("Enter a alphanumeric string with digits and special characters");
obj.input(ob.nextLine());
obj.check_print();
}}
shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 6: Class as the Basis of all Computation (Objects and Classes) - EXERCISES [पृष्ठ ३८८]

APPEARS IN

अविचल Computer Applications [English] Class 10 ICSE
पाठ 6 Class as the Basis of all Computation (Objects and Classes)
EXERCISES | Q VI. 14. | पृष्ठ ३८८
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×