0% found this document useful (0 votes)
5 views6 pages

String Programs

The document contains a series of Java programs that address various string manipulation tasks, including checking for palindromes, counting characters, reversing words, finding the longest and shortest words, capitalizing the first letter of each word, and more. Each program includes a brief description of its purpose and the corresponding code implementation. The years of occurrence for each task are also noted, indicating when they were relevant.

Uploaded by

lol
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)
5 views6 pages

String Programs

The document contains a series of Java programs that address various string manipulation tasks, including checking for palindromes, counting characters, reversing words, finding the longest and shortest words, capitalizing the first letter of each word, and more. Each program includes a brief description of its purpose and the corresponding code implementation. The years of occurrence for each task are also noted, indicating when they were relevant.

Uploaded by

lol
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

1.

Palindrome String
Year of occurrence: 2015, 2019, 2023

Question
Write a program to accept a string and check whether it is a Palindrome.

Solution
import [Link];
class PalindromeString
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s, rev = "";

[Link]("Enter a string:");
s = [Link]();

for(int i = [Link]() - 1; i >= 0; i--)


rev = rev + [Link](i);

if([Link](rev))
[Link]("Palindrome String");
else
[Link]("Not a Palindrome String");
}
}

2. Count Vowels, Consonants, Digits and Spaces


Year of occurrence: 2014, 2018, 2022

Question
Write a program to count vowels, consonants, digits and spaces in a string.

Solution
import [Link];
class CountCharacters
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s;
int v = 0, c = 0, d = 0, sp = 0;

[Link]("Enter a string:");
s = [Link]();

for(int i = 0; i < [Link](); i++)


{
char ch = [Link](i);

if([Link](ch))
d++;
else if(ch == ' ')
sp++;
else if("AEIOUaeiou".indexOf(ch) != -1)
v++;
else
c++;
}

[Link]("Vowels: " + v);


[Link]("Consonants: " + c);
[Link]("Digits: " + d);
[Link]("Spaces: " + sp);
}
}

3. Reverse Each Word of a Sentence


Year of occurrence: 2016, 2020

Question
Write a program to reverse each word of a sentence.

Solution
import [Link];
class ReverseEachWord
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s, word = "", rev;

[Link]("Enter a sentence:");
s = [Link]() + " ";

for(int i = 0; i < [Link](); i++)


{
char ch = [Link](i);

if(ch != ' ')


word = word + ch;
else
{
rev = "";
for(int j = [Link]() - 1; j >= 0; j--)
rev = rev + [Link](j);

[Link](rev + " ");


word = "";
}
}
}
}

4. Longest Word in a Sentence


Year of occurrence: 2017, 2021, 2024 (Specimen)

Solution
import [Link];
class LongestWord
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s, word = "", max = "";

[Link]("Enter a sentence:");
s = [Link]() + " ";

for(int i = 0; i < [Link](); i++)


{
char ch = [Link](i);

if(ch != ' ')


word = word + ch;
else
{
if([Link]() > [Link]())
max = word;
word = "";
}
}

[Link]("Longest word: " + max);


}
}

5. First Letter of Each Word Capital


Year of occurrence: 2018, 2023

Solution
import [Link];
class TitleCase
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s, result = "";

[Link]("Enter a sentence:");
s = [Link]();

result = result + [Link]([Link](0));

for(int i = 1; i < [Link](); i++)


{
if([Link](i - 1) == ' ')
result += [Link]([Link](i));
else
result += [Link](i);
}

[Link](result);
}
}
6. Frequency of a Character
Year of occurrence: 2014, 2019, 2022

Solution
import [Link];
class CharacterFrequency
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s;
char ch;
int count = 0;

[Link]("Enter a string:");
s = [Link]();

[Link]("Enter character:");
ch = [Link]().charAt(0);

for(int i = 0; i < [Link](); i++)


if([Link](i) == ch)
count++;

[Link]("Frequency: " + count);


}
}

7. Anagram Strings
Year of occurrence: 2016, 2020, 2024 (Specimen)

Solution
import [Link];
class Anagram
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s1, s2;
int sum1 = 0, sum2 = 0;

[Link]("Enter first string:");


s1 = [Link]();

[Link]("Enter second string:");


s2 = [Link]();

if([Link]() != [Link]())
{
[Link]("Not Anagram");
return;
}

for(int i = 0; i < [Link](); i++)


{
sum1 += [Link](i);
sum2 += [Link](i);
}
if(sum1 == sum2)
[Link]("Anagram Strings");
else
[Link]("Not Anagram Strings");
}
}

8. Remove Extra Spaces


Year of occurrence: 2017, 2021

Solution
import [Link];
class RemoveExtraSpaces
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s, result = "";

[Link]("Enter a sentence:");
s = [Link]().trim();

for(int i = 0; i < [Link]() - 1; i++)


{
if(!([Link](i) == ' ' && [Link](i + 1) == ' '))
result += [Link](i);
}

result += [Link]([Link]() - 1);


[Link](result);
}
}

9. Display Each Character on New Line


Year of occurrence: 2013, 2018

Solution
import [Link];
class DisplayCharacters
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s;

[Link]("Enter a string:");
s = [Link]();

for(int i = 0; i < [Link](); i++)


[Link]([Link](i));
}
}
10. Shortest Word in a Sentence
Year of occurrence: 2015, 2023

Solution
import [Link];
class ShortestWord
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
String s, word = "", min = "";

[Link]("Enter a sentence:");
s = [Link]() + " ";

for(int i = 0; i < [Link](); i++)


{
char ch = [Link](i);

if(ch != ' ')


word += ch;
else
{
if([Link]("") || [Link]() < [Link]())
min = word;
word = "";
}
}

[Link]("Shortest word: " + min);


}
}

You might also like