The second assignment was to take a line of text and display it in upper case, lower case, count the number of vowels, count the number of consonants, and print in reverse.
Java
import java.util.Scanner;
public class TextManipulation {
public static void main(String[] args) {
System.out.println("Enter word, phrase, or sentence");
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
String upper = upper(text);
System.out.println("Upper= "+upper);
String lower = lower(text);
System.out.println("Lower= " + lower);
int vowelCount = countVowels(text);
System.out.println("Number of vowels="+vowelCount);
int consonantCount = countConsonants(text);
System.out.println("Number of consonants="+consonantCount);
String reverse = reverse(text);
System.out.println("Reverse="+reverse);
}
static String upper(String input){
StringBuffer upper = new StringBuffer();
for (int i=0;i<input.length();i++){
char ch = input.charAt(i);
if (ch>= 'a' && ch <='z'){
ch -= 32;
}
upper.append(ch);
}
return upper.toString();
}
static String lower(String input){
StringBuffer lower = new StringBuffer();
for (int i=0;i<input.length();i++){
char ch = input.charAt(i);
if (ch>= 'A' && ch <='Z'){
ch += 32;
}
lower.append(ch);
}
return lower.toString();
}
static boolean isVowel(char ch){
String str = Character.toString(ch);
str = lower(str);
ch = str.charAt(0);
return (ch =='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u');
}
static int countVowels(String input){
int cnt = 0;
for (int i=0;i<input.length();i++){
char ch = input.charAt(i);
if (isVowel(ch)){
cnt++;
}
}
return cnt;
}
static boolean isLetter(char ch){
String str = Character.toString(ch);
str = lower(str);
ch = str.charAt(0);
return (ch>='a' && ch<='z');
}
static int countConsonants(String input){
int cnt = 0;
for (int i=0;i<input.length();i++){
char ch = input.charAt(i);
if (isLetter(ch) && !isVowel(ch)){
cnt++;
}
}
return cnt;
}
static String reverse(String input){
StringBuffer reverse = new StringBuffer();
for (int i= input.length()-1;i>=0;i--){
reverse.append(input.charAt(i));
}
return reverse.toString();
}
}
C#
using System;
using System.Text;
class TextManipulation
{
static void Main(string[] args)
{
Console.WriteLine("Enter a word, phrase, or sentence");
string text = Console.ReadLine();
string upperText = upper(text);
Console.WriteLine("Upper: " + upperText);
string lowerText = lower(text);
Console.WriteLine("Lower: " + lowerText);
int vowelCount = countVowels(text);
Console.WriteLine("Number of vowels=" + vowelCount);
int consonantCount = countConsonants(text);
Console.WriteLine("Number of consonants=" + consonantCount);
string reverseText = reverse(text);
Console.WriteLine("Reverse=" + reverseText);
Console.WriteLine("Press any key to exit...");
Console.ReadLine();
Environment.Exit(0);
}
static string upper(string input)
{
StringBuilder upperStr = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
char ch = input[i];
if (ch >= 'a' && ch <= 'z')
{
int charIdx = (int)ch -32;
ch = (char)charIdx;
}
upperStr.Append(ch);
}
return upperStr.ToString();
}
static string lower(string input)
{
StringBuilder lowerStr = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
char ch = input[i];
if (ch >= 'A' && ch <= 'Z')
{
int charIdx = (int)ch + 32;
ch = (char)charIdx;
}
lowerStr.Append(ch);
}
return lowerStr.ToString();
}
static int countVowels(string input)
{
int cnt = 0;
for (int i = 0; i < input.Length; i++)
{
char ch = input[i];
if (isVowel(ch))
{
cnt++;
}
}
return cnt;
}
static int countConsonants(string input)
{
int cnt = 0;
for (int i = 0; i < input.Length; i++)
{
char ch = input[i];
if (isLetter(ch) && !isVowel(ch))
{
cnt++;
}
}
return cnt;
}
static string reverse(string input)
{
StringBuilder rev = new StringBuilder();
for (int i = input.Length - 1; i >= 0; i--)
{
rev.Append(input[i]);
}
return rev.ToString();
}
static bool isVowel(char ch)
{
string str = Char.ToString(ch);
str = upper(str);
ch = str[0];
return (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U');
}
static bool isLetter(char ch)
{
string str = Char.ToString(ch);
str = upper(str);
ch = str[0];
return (ch >= 'A' && ch <= 'Z');
}
}
C++
#include <iostream>
#include <string>
using namespace std;
string upper(string input);
string lower(string input);
int countVowels(string input);
int countConsonants(string input);
bool isLetter(char ch);
bool isVowel(char ch);
string reverse(string input);
int main(int argc, char* argv[]){
cout << "Enter word, phrase, or sentence" << endl;
string text;
getline(cin,text);
string up = upper(text);
cout << "Upper: " << up << endl;
string low = lower(text);
cout << "Lower: " << low << endl;
int vowelCount = countVowels(text);
cout << "Vowel Count= " << vowelCount << endl;
int consonantCount = countConsonants(text);
cout << "Consonant Count= " << consonantCount << endl;
string rev = reverse(text);
cout << "Reverse= " << rev << endl;
cout << "Press return to exit.." << endl;
getline(cin,text);
return 0;
}
string upper(string input){
for (int i=0;i<input.length();i++){
char ch = input[i];
if (ch >= 'a' && ch <= 'z'){
ch -= 32;
input[i] = ch;
}
}
return input;
}
string lower(string input){
for (int i=0;i<input.length();i++){
char ch = input[i];
if (ch >= 'A' && ch <= 'Z'){
ch += 32;
input[i] = ch;
}
}
return input;
}
int countVowels(string input){
int cnt = 0;
for (int i=0;i<input.length();i++){
char ch = input[i];
if (isVowel(ch)){
cnt++;
}
}
return cnt;
}
int countConsonants(string input){
int cnt = 0;
for (int i=0;i<input.length();i++){
char ch = input[i];
if (isLetter(ch) && !isVowel(ch)){
cnt++;
}
}
return cnt;
}
bool isLetter(char ch){
return (ch >= 'a' && ch <='z') || (ch >='A' && ch <='Z');
}
bool isVowel(char ch){
return (ch == 'a' || ch == 'A' || ch=='e' || ch=='E' || ch=='i' || ch=='I' ||
ch=='o' || ch=='O' || ch=='u' || ch=='U');
}
string reverse(string input){
string temp(input);
for (int i=input.length()-1;i>=0;i--){
int j = input.length()-1-i;
temp[j] = input[i];
}
return temp;
}
Ruby
def upper(input)
upStr = String.new
input.scan(/./) do |c|
if (c >= 'a' && c<='z')
int = c[0] -32
c = int.chr
end
upStr += c
end
return upStr
end
def lower(input)
lowStr = String.new
input.scan(/./) do |c|
if (c >= 'A' && c<='Z')
int = c[0] + 32
c = int.chr
end
lowStr += c
end
return lowStr
end
def isVowel(ch)
low = lower(ch)
return (low == 'a' || low == 'e' || low == 'i' || low == 'o' || low =='u')
end
def countVowels(input)
cnt = 0
input.scan(/./) do |c|
if isVowel(c)
cnt = cnt +1
end
end
return cnt
end
def isLetter(ch)
low = lower(ch)
return (low >= 'a' && low <='z')
end
def countConsonants(input)
cnt = 0
input.scan(/./) do |c|
if (isLetter(c) && !isVowel(c))
cnt = cnt +1
end
end
return cnt
end
def reverse(input)
rev = String.new
sz = input.length
for i in 1 ... sz
rev += input[sz-i-1,1]
end
return rev
end
puts 'Enter a word, phrase, or sentence'
text = gets
textUpper = upper(text)
puts 'Upper: ' + textUpper
textLower = lower(text)
puts 'Lower: ' + textLower
vowelCount = countVowels(text)
puts 'Vowel Count= ' + vowelCount.to_s
consonantsCount = countConsonants(text)
puts 'Consonant Count= ' + consonantsCount.to_s
textRev = reverse(text)
puts 'Reverse: ' + textRev
I started to get a little scared on the C++ front on this one, remembering many difficult labs of yesteryear doing string manipulation in C. But C++'s string class is pretty solid. It should really support append(charcater)... The Ruby was the hardest, though it wound up being very nice looking. The scan(/./) was difficult to figure out. There must be an easier way. I'm actually not sure if this would be appropriate for a beginner. The string[0] to get the ASCII value of a character was also painful, though I liked the chr method on integer for converting back. It's funny that Ruby is one of those vaunted dynamic languages, yet it has a very hard time making conversions that are very easy in strongly typed languages... Finally, I used StringBuffer/StringBuilder for Java/C#. This seemed a little advanced, but I think it is a good idea to teach the use of these very early.
I did the Python version last. It is a new language for me, so it's probably the worst version!
Python
def upper(input):
up = ""
for i in range(len(input)):
ch = input[i]
if ch >= 'a' and ch <='z' :
n = ord(ch) - 32
ch = chr(n)
up += ch
return up
def lower(input):
low = ""
for i in range(len(input)):
ch = input[i]
if ch >= 'A' and ch <='Z' :
n = ord(ch) + 32
ch = chr(n)
low += ch
return low
def isLetter(char):
if lower(char) >= 'a' and lower(char) <='z':
return 1
return 0
def isVowel(char):
return lower(char) in ['a','e','i','o','u']
def countVowels(input):
cnt = 0
for i in range(len(input)):
if isVowel(input[i]):
cnt = cnt +1
return cnt
def countConsonants(input):
cnt = 0
for i in range(len(input)):
if isLetter(input[i]) and not isVowel(input[i]):
cnt = cnt +1
return cnt
def reverse(input):
up = ""
sz = len(input)
for i in range(len(input)):
n = sz - i -1
up += input[n]
return up
text = raw_input("Enter a word, phrase, or sentence\n")
up = upper(text)
print "Upper: " + up
low = lower(text)
print "Lower: " + low
vowelCount = countVowels(text)
print "Vowel count= " + str(vowelCount)
consonantCount = countConsonants(text)
print "Consonant count= " + str(consonantCount)
reverse = reverse(text)
print "Reverse: " + reverse