0% found this document useful (0 votes)
10 views

String programs

The document contains multiple Java class definitions that perform various string manipulations. The classes validate Gmail IDs, convert strings to uppercase while replacing vowels and consonants, count the number of digits, alphabets, and special characters in a string, and count vowels in an uppercase string. Each class includes example inputs and expected outputs for clarity.

Uploaded by

prachir741
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

String programs

The document contains multiple Java class definitions that perform various string manipulations. The classes validate Gmail IDs, convert strings to uppercase while replacing vowels and consonants, count the number of digits, alphabets, and special characters in a string, and count vowels in an uppercase string. Each class includes example inputs and expected outputs for clarity.

Uploaded by

prachir741
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Question 1

Define a class to accept the gmail id and check for its validity. A gmail id is valid
only if it has:
 @
 . (dot)
 gmail
 com
Example: [email protected] is a valid gmail id.
import java.util.*;
public class Validategmail
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter a Gmail ID: ");
String gmailId = sc.nextLine();
int len = gmailId.length();

if (gmailId.endsWith("@gmail.com")&& gmailId.indexOf('@') == len - 10)


{
System.out.println(gmailId + " is a valid Gmail ID.");
}
else
{
System.out.println(gmailId + " is NOT a valid Gmail ID.");
}
}
}
Question 2
Define a class to accept a string and convert the same to uppercase, create and display the new string by replacing each
vowel by immediate next character and every consonant by the previous characters remain the same.
Example : input : #IMAGINTION@2024
Output:#JLBFJMBSJPM@2024
import java.util.*;
class convert
{
public static void main(String args[])
{
Scanner sc= new Scanner(System.in);
System.out.println("Enter string");
String st=sc.nextLine();
int len=st.length();
char ch;
String st1="";
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(Character.isLetter(ch))
{
if(ch == 'A'|| ch == 'E'|| ch == 'I'|| ch == 'O'|| ch == 'U'||ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o'||
ch == 'u')
st1=st1+(char)(ch+1);
else
st1=st1+(char)(ch-1);
}
else
st1=st1+ch;
}
System.out.println("Input="+st);
System.out.println("output="+st1);
}
}
Question 3
Define a class to accept a String and print the number of digits, alphabets and special characters in the string.
Example:
S = "KAPILDEV@83"
Output:
Number of digits – 2
Number of Alphabets – 8
Number of Special characters – 1
import java.util.Scanner;
public class frequency
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a string:");
String st= sc.nextLine();
int len = st.length();
int alpha = 0,dig = 0,sp = 0;
char ch;

for (int i = 0; i < len; i++)


{
ch = str.charAt(i);
if (Character.isLetter(ch))
alpha++;
else if (Character.isDigit(ch))
dig++;
else if (!Character.isWhitespace(ch))
sp++;
}

System.out.println("No. of Digits = " + dig);


System.out.println("No. of Alphabets = " + alpha);
System.out.println("No. of Special Characters = " + sp);
}
}
Question 4
Define a class to accept a string and convert it into uppercase. Count and display the number of vowels in it.
Input: robotics
Output: ROBOTICS Number of vowels: 3
import java.util.*;
public class frequency_vowel
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter strong");
String st = sc.nextLine();
st=st.toUpperCase();
int len=st.length();
char ch;int count=0;
for(int i=0;i<len;i++)
{
ch=st.charAt(i);
if(ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')
{
count++;
}
}
System.out.println("Frequency of vowels="+count);
}
}

You might also like