ICSE JAVA PROGRAMS 2019: SECTION B QUESTION 8
Programming for school students ( upper to middle/secondary school)
Write a program to input s sentence and convert it into uppercase and display the total number of words starting with letter 'A'.
Example:
INPUT: Advancement and application of Information Technology are ever changing.
OUTPUT: ADVANCEMENT AND APPLICATION OF INFORMATION TECHNOLOGY ARE EVER CHANGING.
Number of word(s)starting with A is/are: 4
JAVA CODE
import java.util.Scanner;
public class ISCJava{
public static void main(String[] args) {
String sentence;
int counterStartingA = 0;
Scanner in = new Scanner(System.in);
System.out.println("Enter your sentence:");
sentence = in.nextLine();
System.out.println();
System.out.println("OUTPUT:");
System.out.println("ORIGINAL STATEMENT:");
System.out.println(sentence);
System.out.println();
System.out.println("AFTER CHANGING TO UPPER CASE:");
for(int i =0; i< sentence.length();i++) {
char c = sentence.charAt(i);
if(c >= 'a' && c <= 'z') {
c -= 32;
System.out.print(c);
}
else
System.out.print(c);
}
//counting number of words staring with A
System.out.println();
for(int i =0; i< sentence.length(); i++)
if (sentence.charAt(i) == 'A' || sentence.charAt(i) == 'a') {
if(i==0)
counterStartingA++;
else if(sentence.charAt(i-1) == ' ')
counterStartingA++;
}
System.out.println("Number of word(s)starting with A is/are: "+counterStartingA);
in.close();
}
}
PYTHON CODE
counterStartingA = 0
sentence = str(input("Enter the sentence: "))
print("OUTPUT:")
print("ORIGINAL STATEMENT:")
print(sentence)
print("AFTER CHANGING TO UPPER CASE:")
for i in range(len(sentence)):
c = (sentence[i])
if (c >= 'a' and c <= 'z'):
t = ord(c) -32
print(chr(t),end="")
else:
print(c,end="")
for i in range(len(sentence)):
if sentence[i] == 'A' or sentence[i] == 'a':
if i == 0:
counterStartingA +=1
elif sentence[i-1] == ' ':
counterStartingA +=1
print()
print("Number of word(s)starting with A is/are:",counterStartingA)
No comments:
Post a Comment