Exchange First and Last Letter of Each Word Program in Java and Python
ISC 2013 Question 9 solution to interchange the first and last alphabet of each word in a sentence using Java and Python.
Question:
Design a class Exchange to accept a sentence and interchange the first alphabet with the last alphabet for each word in the sentence, with single-letter words remaining unchanged. The words in the input sentence are separated by a single blank space and terminated by a full stop.
Example
Some of the data members and member functions are given below:
Specify the class Exchange giving details of the constructor, void readsentence(), void exfirstlast() and void display(). Define the main() function to create an object and call the functions accordingly to enable the task.
Algorithm:
Step 1: Start.
Step 2: Define a class named Exchange.
Step 3: Declare string variables sent and rev, and integer variable size.
Step 4: In the constructor, initialize sent and rev to empty strings and size to 0.
Step 5: In readsentence(), accept a sentence from the user.
Step 6: Store the length of the sentence in size.
Step 7: If the last character is not a full stop, append a full stop and increase size by 1.
Step 8: In exfirstlast(), initialize a temporary word string s1 as empty.
Step 9: Traverse the sentence character by character.
Step 10: If the current character is neither a blank space nor a full stop, add it to s1.
Step 11: When a blank space or full stop is found, process the complete word stored in s1.
Step 12: For the first character position of the word, add the last character to rev.
Step 13: For the last character position of the word, add the first character to rev.
Step 14: For all middle positions, add the original character to rev.
Step 15: Add a blank space after each changed word and reset s1 to an empty string.
Step 16: In display(), print the original sentence and the changed sentence.
Step 17: Stop.
Explanation:
This program changes every word in a sentence by interchanging its first and last letters. The class Exchange uses three data members. The variable sent stores the original sentence, rev stores the changed sentence, and size stores the length of the sentence. The constructor gives these variables initial values so that the object starts with an empty original sentence, an empty changed sentence and size 0.
The readsentence() method accepts the sentence from the user. The question says that the sentence is terminated by a full stop. The original logic also handles a sentence that does not end with a full stop by adding one at the end. This makes the later word-extraction loop easier because the program treats both a blank space and a full stop as word-ending characters. Once a full stop is present, the loop can process the last word in exactly the same way as the earlier words.
The method exfirstlast() scans the sentence one character at a time. Characters that are not spaces or full stops are collected in the temporary string s1. When a space or full stop is reached, s1 contains one complete word. The program then runs another loop over this word. If the loop is at the first position, it takes the last character of the word. If it is at the last position, it takes the first character. All middle characters are copied unchanged. For a single-letter word, the first and last position are effectively the same, so the word remains unchanged.
After each word is processed, a blank space is added to the changed sentence and the temporary word is cleared. Finally, display() prints both the original sentence and the changed sentence. For example, warm becomes marw, and day becomes yad.
Java Program:
/**
* The class Exchange inputs a sentence and interchanges the first
* alphabet with the last alphabet for each word in the sentence.
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
* @Question Year : ISC Theory 2013 Question 9
*/
import java.util.Scanner;
class Exchange
{
static Scanner sc = new Scanner(System.in);
String sent;
String rev;
int size;
Exchange()
{
sent = "";
rev = "";
size = 0;
}
void readsentence()
{
System.out.print("Enter a sentence : ");
sent = sc.nextLine();
size = sent.length();
// Add a full stop if it is not present at the end.
if(sent.charAt(size - 1) != '.')
{
sent = sent + ".";
size = size + 1;
}
}
void exfirstlast()
{
String s1 = "";
for(int i = 0; i < size; i++)
{
char ch = sent.charAt(i);
if(ch != ' ' && ch != '.')
{
s1 = s1 + ch;
}
else
{
int l = s1.length();
// Interchange first and last characters of the word.
for(int j = 0; j < l; j++)
{
if(j == 0)
ch = s1.charAt(l - 1);
else if(j == l - 1)
ch = s1.charAt(0);
else
ch = s1.charAt(j);
rev = rev + ch;
}
rev = rev + " ";
s1 = "";
}
}
}
void display()
{
System.out.println("The Original Sentence is : " + sent);
System.out.println("The Changed Sentence is : " + rev.trim());
}
public static void main(String args[])
{
Exchange ob = new Exchange();
ob.readsentence();
ob.exfirstlast();
ob.display();
}
}Equivalent Python Program:
# Program to interchange the first and last letter of each word.
class Exchange:
def __init__(self):
self.sent = ""
self.rev = ""
self.size = 0
def readsentence(self):
self.sent = input("Enter a sentence : ")
self.size = len(self.sent)
# Add a full stop if it is not present at the end.
if self.sent[-1] != ".":
self.sent = self.sent + "."
self.size = self.size + 1
def exfirstlast(self):
s1 = ""
for ch in self.sent:
if ch != " " and ch != ".":
s1 = s1 + ch
else:
changed = ""
# Interchange first and last characters of the word.
for j in range(len(s1)):
if j == 0:
changed = changed + s1[-1]
elif j == len(s1) - 1:
changed = changed + s1[0]
else:
changed = changed + s1[j]
self.rev = self.rev + changed + " "
s1 = ""
def display(self):
print("The Original Sentence is :", self.sent)
print("The Changed Sentence is :", self.rev.strip())
ob = Exchange()
ob.readsentence()
ob.exfirstlast()
ob.display()Output:
Leave a Reply
Your email address will not be published. Comments are reviewed before appearing publicly.