File: Untitled Document 1 Page 1 of 5
import java.util.Scanner;
/**
* Write a description of class my_strings here.
*
* @author (Panav)
* @class (10A)
*/
public class my_strings
{
// This function checks if the number in the string is divisible by 2 or not and
returns boolean value
boolean chkDiv2 (String s)
{
char c;
int num = s.length() - 1;
c = s.charAt(num);
String s2 = c + "";
int finalnum;
finalnum = Integer.parseInt(s2);
System.out.print(finalnum%2 == 0);
return finalnum%2 == 0;
/**
* Output:
* true
*/
}
// This function checks if the number in the string is divisible by 3 or not and
returns boolean value
boolean chkDiv3 (String s)
{
char c;
int acc = 0;
int num = 0;
while (acc < s.length())
{
c = s.charAt(acc);
num = num + (Integer.parseInt((c + "" )));
acc +=1;
}
return num%3 == 0;
/**
* Output:
* true
*/
}
// This function checks if the number in the string is divisible by 6 or not and
returns boolean value
boolean chkDiv6 (String num)
{
return (chkDiv2(num) == true && chkDiv3(num) == true);
/**
* Output:
* true
*/
}
//This function capitalises I and even the first letter of a sentence
String correctGrammar (String s)
{
char c;
int acc = 2;
String s2 = "";
char previous_char = s.charAt(acc - 1);
char previous_char2 = s.charAt(acc - 2);
File: Untitled Document 1 Page 2 of 5
String final_string = (Character.toUpperCase (s.charAt(0))) + "";
while (acc < s.length())
{
c = s.charAt(acc);
previous_char = s.charAt(acc - 1);
previous_char2 = s.charAt(acc - 2);
if (c == 'i' && previous_char == ' ' && s.charAt(acc + 1 ) == ' ')
{
c = Character.toUpperCase(c);
s2 = s2 + c;
}
else if ((previous_char == ' ' && previous_char2 == '.') ||(previous_char == '
' && previous_char2 == '?' )|| (previous_char == ' '&& previous_char2 =='!'))
{
c = Character.toUpperCase(c);
s2 = s2 + c;
}
else
s2 = s2 + c;
acc += 1;
}
System.out.print(final_string + s.charAt(1) + s2);
return final_string + s.charAt(1) + s2;
/**
* Output:
* Hello I am Panav. My
*/
}
// This function correctly capitalizes a word according to rules of English Grammar
String capitalize_word ( String s)
{
char c ;
int acc = 1;
char previous_char;
String final_string = "" ;
String s2 = (Character.toUpperCase (s.charAt(0))) + "";
while (acc < s.length())
{
c = s.charAt(acc);
previous_char = s.charAt(acc - 1);
if (previous_char == ' ')
final_string = final_string + (Character.toUpperCase(c));
else
final_string = final_string + c;
acc += 1;
}
System.out.print(s2 + final_string);
return s2 + final_string;
/**
* Output:
* Hello
*/
}
//This function reverses a string
String reverseString (String s)
{
s = s.trim();
int acc = (s.length())-1;
char c;
String s2 = "";
while (acc >= 0)
{
c = s.charAt(acc);
s2= s2 + c;
File: Untitled Document 1 Page 3 of 5
acc = acc - 1;
}
System.out.print(s2);
return s2;
/**
* Output:
* DLROW OLLEH
*/
}
//This function checks if the given string is a palindrome and returns a boolean value
boolean chkpalindrome (String s)
{
String s1 = s.toUpperCase();
System.out.println (s1);
System.out.print (reverseString(s1));
return s1.equals(reverseString(s1));
/**
* Output:
* true
*/
}
//This function capitalises each letter in a word except the first and last letter
String firstLast_capital (String s)
{
int acc = 1;
char c;
String s2 ="";
String s3 = (Character.toLowerCase (s.charAt(0))) + "";
String s4 = (Character.toLowerCase(s.charAt(s.length()-1))) + "" ;
while (acc < (s.length()- 1))
{
c= s.charAt(acc);
if ((s.charAt(acc-1)) == ' ' || ((s.charAt(acc + 1)) == ' '))
{
c = Character.toLowerCase(c);
}
else
c = Character.toUpperCase(c);
s2 = s2 + c;
acc += 1;
}
System.out.print(s3+s2+s4);
return s3 + s2 + s4;
/**
* Output:
* pANAv
*/
}
//This function removes the spaces in a given string
String removeSpace (String s)
{
int acc = 0;
char c;
String s1 = "";
int space_value = s.indexOf(' ');
String s_final= "";
String s2 = "";
String s3="";
while (space_value != -1)
{
s1 = s.substring(0 , space_value);
s = s1 + s.substring ((space_value+1));
//s2 = s2 + s1;
File: Untitled Document 1 Page 4 of 5
space_value = s.indexOf(' ');
//acc = space_value + 1;
}
return s.trim();
/**
* Output:
* Hello
*/
}
//This function capitalises the alternate letter of a word in a given string
String alternateCapital (String s)
{
String s1 = "";
int acc = 0;
char c;
int n = 0;
while ( acc < s.length())
{
c = s.charAt(acc);
if (c == ' ')
s1 = s1 + ' ' ;
else if (n % 2 == 0)
{
c = Character.toUpperCase(c);
n += 1;
s1 = s1 + c;
}
else
{
c = Character.toLowerCase(c);
n += 1;
s1 = s1 + c;
}
acc += 1;
}
System.out.print(s1);
return s1;
/**
* Output:
* PaNaV
*/
}
//This function corrects the article in a string according to the rules of English
grammar
String correctArticle (String s)
{
int v = s.indexOf(" a ") + 1;
int c = s.indexOf (" an ") + 1;
while ((v-1) != -1 && (c-1) != -1 )
{
if (s.charAt(v + 2) == 'a' || s.charAt(v + 2) == 'e' || s.charAt(v + 2) == 'i'
|| s.charAt(v + 2) == 'o' || s.charAt(v + 2) == 'u')
{
s = s.substring(0 , v) + "an " + s.substring(v+2);
}
v = s.indexOf(" a ") + 1;
if (s.charAt(c + 2) != 'a' || s.charAt(c + 2) != 'e' || s.charAt(c + 2) != 'i'
|| s.charAt(c + 2) != 'o' || s.charAt(c + 2) != 'u')
{
s = s.substring(0 , c) + "a " + s.substring(c+2);
}
}
System.out.print(s);
return s;
File: Untitled Document 1 Page 5 of 5
/**
* Output:
* He is an intelligent boy
*/
}
//This function prints the pattern of the square of the given input
void printSquare (int n)
{
int acc = 0;
int acc2 = 0;
while(acc < n)
{
acc2 = 0;
System.out.print ("*");
while (acc2<n-1)
{
System.out.print("*");
acc2 +=1;
}
System.out.println();
acc+=1;
}
/**
* Output:
***
***
***
*/
}
}