0% found this document useful (0 votes)
8 views13 pages

Computer String

The document contains multiple Java programs that perform various string manipulations, including printing initials, checking for palindromes, reversing letter cases, counting digits and special characters, finding the longest word in a sentence, and capitalizing the first letter of each word. Each program includes variable declarations, input handling, and logic to achieve the desired output. Additionally, variable descriptions are provided for clarity on their purpose.

Uploaded by

7929
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views13 pages

Computer String

The document contains multiple Java programs that perform various string manipulations, including printing initials, checking for palindromes, reversing letter cases, counting digits and special characters, finding the longest word in a sentence, and capitalizing the first letter of each word. Each program includes variable declarations, input handling, and logic to achieve the desired output. Additionally, variable descriptions are provided for clarity on their purpose.

Uploaded by

7929
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

1.​ Write a program to accept a name and print its initials.

Example,
Parameter-> AJAY PRATAP SINGH
Output-> A.P.S.

import java.util.*;
public class sd
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Declaring the variables
String n;
int l,i;
//Taking the full name and trimming it
System.out.println("Enter your full name");
n=in.nextLine();
n=n.trim();
//Adding a space and getting the length
n=" "+n;
l=n.length();
//Getting the initials
for(i=0;i<l;i++)
{
if(n.charAt(i) == ' ' && (i + 1) < l && n.charAt(i + 1) != ' ')
System.out.print(n.charAt(i+1)+".");
}
}
}
Sample Input/Output

Variable description

Name of the variable Data type Purpose

n String To store the name

l int To store the length

i int Looping variable


2.Write a program to check whether a string is a Palindrome or not. A
Palindrome is a string that reads the same from left to right and vice versa.
E.g. MADAM, ARORA, ABBA, etc.

import java.util.*;
public class sd
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Declaring the variables
String n,m=" ";
int l;
//Getting the name and trim with the length
System.out.println("Enter a name");
n=in.nextLine();
n=n.trim();
l=n.length();
l=l-1;
//reversing the name
for(;l>=0;l--)
{
m= m+n.charAt(l);
}
//Showing the reverse name
m=m.trim();
System.out.print(m);
}
}
Sample input/output

Variable description

Name of the variable Data type Purpose

n String To store the name

m String To store the reverse name

l int To store the length of the name


3)Write a program to input a string and print out the text with the uppercase
and lowercase letters reversed, but all other characters should remain the
same as before.
Example :
INPUT : WelComE TO School
OUTPUT : wELcOMe to sCHOOL

import java.util.*;
public class sd
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Declaring the variable
String n,a=" ";
char m;
int l, i;
//Getting the word.Trimming it and getting its length.
System.out.println("Enter a word");
n=in.nextLine();
n=n.trim();
l=n.length();
//Getting the letters in the variables and changing the case.
for(i=0;i<l;i++)
{
m=n.charAt(i);
if(Character.isUpperCase(m))
{
a= a+Character.toLowerCase(m);
}
else if (Character.isLowerCase(m))
{
a=a+Character.toUpperCase(m);
}
}
System.out.print(a);
}
}
Variable description

Name of the variable Data type Purpose

n String To store the word

a String To store the word after The changes

m char To store single character of the whole word

l int To store the length of the word

i int Looping variable

Sample Input/Output
4)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.*;
public class sd
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Declaring and initializing the variable
String s;
int i,l,d=0,a=0,sp=0;
char ch;
System.out.println("Enter any word");
//Getting the word and then trimming it and getting its length
s=in.nextLine();
s=s.trim();
l=s.length();
//Calculating the number of digit,alphabet and special character
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(Character.isDigit(ch))
d++;
else if(Character.isLetter(ch))
a++;
else
sp++;
}
System.out.println("Number of digits"+d);
System.out.println("Number of Alphabets"+a);
System.out.println("Number of special character"+sp);
}}
Sample Input/Output

Variable description

Name of the variable Data type Purpose

s String To store the word

ch char To store a single character from the string

i int Looping variable

l int To store the length

a int To store the number of letter

d int To store the number digit

sp int To store the number of special character


5)Write a program to input a sentence and print the number of characters
found in the longest word and the word of the given sentence. For example if
S= “India is my country” then the output should be 7 and Country.

import java.util.*;
public class sd
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
//Declaring and initializing the variables
String s,word="",mword="";
int start=0,max=0,i,l;
char ch;
System.out.println("Enter any word");
//getting the word and trimming it and getting the length
s=in.nextLine();
s=s.trim();
s=s+" ";
l=s.length();
//Getting the biggest word and its length
for(i=0;i<l;i++)
{
if(s.charAt(i)==' ')
{
ch=s.charAt(i);
word=s.substring(start,i);
if(word.length()>max)
{
max=word.length();
mword=word;
}
start=i+1;
}
}
System.out.println("The max length is "+max);
System.out.println("The max word is "+mword);
}}
Sample input/Output

Variable description

Name of the variable Data type Purpose


s String To store the sentence
word String To store a single word
mword String To store the biggest word
i int Looping variable
l int To store the length
max int To store the maximum length
start int To store the index number of the
substring to start
ch char To store the single character of the
sentence
6)Write a program in Java to accept a string in lowercase and change the first letter
of every word to uppercase. Display the new string.
Sample input: we are in cyber world
Sample output: We Are In Cyber World

import java.util.*;
public class sd
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String s;
int i,l;
char ch;
System.out.println("Enter any word");
s=in.nextLine();
s=s.trim();
s=" "+s;
l=s.length();
for(i=0;i<l;i++)
{
ch=s.charAt(i);
if(ch==' ')
{
ch=Character.toUpperCase(s.charAt(i+1));
s=s.toUpperCase(s.charAt(i+1),ch);
}
}
s=s.trim();
System.out.println(s);
}}

Variable description

Name of the variable Data type Purpose

s String To store the sentence

ch char To store a single character


of the string

i int Looping variable

l int To store the length of the


string

You might also like