You are on page 1of 6

/*

* Source program for String Theory for CS 1A


* Written by Nachi Poupin, 10/23/2017
*/

import java.util.Scanner;

public class Foothill


{
public static void main(String[] args)
{
//Call input methods
char keyCharacter = getKeyCharacter();
String theString = getString();
//Call to computation methods, print output
String maskString = maskCharacter(theString, keyCharacter);
System.out.print("\nYour phrase with " + keyCharacter
+ " 'masked' is: " + maskString);
String removedString = removeCharacter(theString, keyCharacter);
System.out.print("\nYour phrase with " + keyCharacter
+ " removed is: " + removedString);
int count = countKey(theString, keyCharacter);
System.out.print("\nThe number of " + keyCharacter
+ "'s in your phrase are " + count);

} //End of main

//getKeyCharacter method definition


public static char getKeyCharacter()
{
//Declare variables
Scanner input = new Scanner(System.in);
String instructions, strKeyCharacter;
strKeyCharacter = "";
int numbCharacters = 0;
char keyCharacter = ' ';

//Test to make sure user only enters one char


//until they get it right
while (numbCharacters != 1)
{
instructions = "Please enter a SINGLE character "
+ "to act as key: ";
System.out.print(instructions); Did not close input
strKeyCharacter = input.nextLine(); stream
numbCharacters = strKeyCharacter.length(); -1
keyCharacter = strKeyCharacter.charAt(0);
}
//Return key character to main()
return keyCharacter;
}

//getString method definition


public static String getString()
{
//Declare variables
Scanner input = new Scanner(System.in);
String instructions, theString;
theString = "";
int n, NUMBCHARS;
n = 0;
NUMBCHARS = 4;
//Test to make sure user enters a string of >=4 characters
//until they get it right
while (n < NUMBCHARS)
{
instructions = "Please enter a phrase or sentence "
+ "with 4 or more characters: ";
System.out.print(instructions);
theString = input.nextLine();
n = theString.length(); Magic number
} -2
input.close();
//Return string to main()
return theString;
}

//maskCharacter method definition


public static String maskCharacter(String theString,
char keyCharacter)
{
//Define new empty string
String maskCharacter = "";
//Add every character from user's string to new string
//replacing every occurrence of the key character with $
for (int c = 0; c < theString.length(); c++)
if (theString.charAt(c) != keyCharacter)
maskCharacter += theString.charAt(c);
else
maskCharacter += "$";

//Return string with masked character to main()


return maskCharacter;
}

//removeCharacter method definition


public static String removeCharacter(String theString,
char keyCharacter)
{
//Define new empty string
String removeCharacter = "";
//Add every character from user's string to new string
//omitting every occurrence of the key character
for (int c = 0; c < theString.length(); c++)
if (theString.charAt(c) != keyCharacter)
removeCharacter += theString.charAt(c);
else
removeCharacter += ""; Unnecessary code

//Return string with key character removed to main()


return removeCharacter;
}

//countKey method definition


public static int countKey(String theString, char keyCharacter)
{
//Initialize counter at zero key characters
int countKey = 0;
//Count every occurrence of the key character in the string
for (int i = 0; i < theString.length(); i++)
if (theString.charAt(i) == keyCharacter)
countKey ++;
//Return the amount of occurrences of the key character
//in the string to main()
return countKey;
}
}
/*-----------------Sample Run#1 --------------------------------
Please enter a SINGLE character to act as key: paralelepipedo
Please enter a SINGLE character to act as key: e
Please enter a phrase or sentence with 4 or more characters:
paralelepipedo

Your phrase with e 'masked' is: paral$l$pip$do


Your phrase with e removed is: parallpipdo
The number of e's in your phrase are 3
-------------------End Sample Run #1----------------------------*/
/*-----------------Sample Run #2 -------------------------------
Please enter a SINGLE character to act as key: A
Please enter a phrase or sentence with 4 or more characters: Ana
Please enter a phrase or sentence with 4 or more characters: AnacondA

Your phrase with A 'masked' is: $nacond$


Your phrase with A removed is: nacond
The number of A's in your phrase are 2
-------------------End Sample Run#2 ____________________________*/
/*-----------------Sample Run #3 -------------------------------
Please enter a SINGLE character to act as key: fafafafafafa
Please enter a SINGLE character to act as key: aaaaaaaaa
Please enter a SINGLE character to act as key: f
Please enter a phrase or sentence with 4 or more characters: faf
Please enter a phrase or sentence with 4 or more characters: Falafel

Your phrase with f 'masked' is: Fala$el


Your phrase with f removed is: Falael
The number of f's in your phrase are 1
-------------------End Sample Run#3 ____________________________*/
/*-----------------Sample Run #4 -------------------------------
Please enter a SINGLE character to act as key: w
Please enter a phrase or sentence with 4 or more characters:
Where were they when we left

Your phrase with w 'masked' is: Where $ere they $hen $e left
Your phrase with w removed is: Where ere they hen e left
The number of w's in your phrase are 3
-------------------End Sample Run#4 ____________________________*/
package edu.foothill.labs_cs1a;

/// Instructor Solution:


//Original - Prof. Loceff, Updates, Edits, Annotations: &
//
//Notes:
//- Use of sensible names for vars
//- Correct use of global consts
//- Use of symbolic consts rather than literals (magics)
//- Avoidance of library methods: .erase, .count, .find, .replace*
//- Faithfulness to spec
//
import java.util.Scanner;

public class Foothill_Lab_05_StringTheory


{
static final int MIN_STR_LEN = 4;
static final int MAX_STR_LEN = 500; // sanity check
static final char MASK_CHAR = '$';
static final char REQUIRED_KEY_LENGTH = 1;
static Scanner scanner;

public static void main(String[] args) {


String userString;
char keyCharacter;

scanner = new Scanner(System.in);

// get the string from the user


keyCharacter = getKeyCharacter();
userString = getString();

// now do the various string processes


System.out.println("\nString with '" + keyCharacter + "' masked: "
+ maskCharacter(userString, keyCharacter));

System.out.println( "\n# " + keyCharacter + "s: "


+ countKey(userString, keyCharacter) );

System.out.println("\nString with '" + keyCharacter + "' removed: "


+ removeCharacter(userString, keyCharacter) );

scanner.close();
}

// method definitions:
public static char getKeyCharacter() {
String str;

do {
System.out.print("Please enter a SINGLE character to act as key: ");
str = scanner.nextLine();
} while (str.length() != REQUIRED_KEY_LENGTH);
return str.charAt(0);
}

public static String getString() {


String str;

do {
System.out.println("Please enter a phrase or sentence "
+ ">= "+ MIN_STR_LEN
+ " and <= " + MAX_STR_LEN + " characters: " );
str = scanner.nextLine();
} while ( str.length() < MIN_STR_LEN || str.length() > MAX_STR_LEN );

return str;
}

public static String maskCharacter(String str, char keyChar) {


String result = "";

// Note use of tertiary operator to condense the IF statement


for (int i = 0; i < str.length(); i++)
result += (str.charAt(i) == keyChar? MASK_CHAR : str.charAt(i));

return result;
}

public static String removeCharacter(String str, char keyChar) {


String result = "";

for (int i = 0; i < str.length(); i++)


if (str.charAt(i) != keyChar)
result = result + str.charAt(i);

return result;
}

public static int countKey(String str, char keyChar) {


int count = 0;

for (int i = 0; i < str.length(); i++)


if (str.charAt(i) == keyChar)
count++;
return count;
}
}

/* ---------------------- Sample run ---------------------------------------

Please enter a SINGLE character to act as key: f


Please enter a phrase or sentence >= 4 and < 500 characters:
dfg
Please enter a phrase or sentence >= 4 and < 500 characters:
dfg dfg dfg dfg dfg df gd fg

String with 'f' masked:


d$g d$g d$g d$g d$g d$ gd $g

# fs: 7

String with 'f' removed:


dg dg dg dg dg d gd g

(more runs supplied by student)

------------------------------------------------------------------------ */

You might also like