Question 1. Write a Porgram to Print no.of Spaces and Characters in a given input input: Hello This is ABCD from XYZ city output : No of Spaces : 6 and characters : 26
import java.util.*;
class Solution
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
int space_count=0,char_count=0;
System.out.println("Enter the string");
String str=in.nextLine();
for(int i=0;i<str.length();i++)
{
if(str.charAt(i) == ' ')
{
space_count++;
}
else
{
char_count++;
}
}
System.out.println("No .of characters:"+char_count);
System.out.println("No.of Space="+space_count);
}
}
output:
Enter the string
Hello This is ABCD from XYZ city
No .of characters:26
No.of Space=6
class Solution
{
public static void main(String [] args)
{
Scanner in = new Scanner(System.in);
int space_count=0,char_count=0;
System.out.println("Enter the string");
String str=in.nextLine();
for(int i=0;i<str.length();i++)
{
if(str.charAt(i) == ' ')
{
space_count++;
}
else
{
char_count++;
}
}
System.out.println("No .of characters:"+char_count);
System.out.println("No.of Space="+space_count);
}
}
output:
Enter the string
Hello This is ABCD from XYZ city
No .of characters:26
No.of Space=6
Comments
Post a Comment