Find the number of special characters in my Character array - java

I want to count how many special characters are there in my Character array.
This is my code so far:
String s = "hi .this :) is test +line";
int len = s.length();
Character[] array = new Character[len];
for (int i = 0; i < len ; i++)
{
if(array[i]==' ' || array[i]==':' || array[i]=='.' || array[i]=='\'' || array[i]=='\"' || array[i]==')')
special_chars++
}

You can use the methods of Character class.
if(!Character.isLetterOrDigit(array[i]) && !Character.isWhitespace(array[i]))
special_chars++;
Character.isLetterOrDigit checks if the char is a letter or a digit. If it is none, then it certainly is a special character!

Create a String of your special characters, loop through every character in your input, and check if it's in the String of special characters. Something like,
String s = "hi .this :) is test +line";
String spec = " :.'\")";
int special_chars = 0;
for (char ch : s.toCharArray()) {
if (spec.indexOf(ch) > -1) {
special_chars++;
}
}

Using a BitSet, you can efficiently set and check indexed binary information (in your case: whether a given character is special or not), so compared to other solutions in this thread you don't need to loop over the set of special characters:
BitSet specialCharacters = new BitSet();
specialCharacters.set(' ');
specialCharacters.set(':');
specialCharacters.set('.');
specialCharacters.set('\'');
specialCharacters.set('\"');
specialCharacters.set(')');
String text = "hi .this :) is test +line";
for (char c : text.toCharArray()) {
if (specialCharacters.get(c)) {
// special character detected
}
}

Related

What is the best way to replace a letter with the letter following it in the alphabet in Java?

I'm a programming newbie and I am doing a coderbyte exercise that says "
Replace every letter in the string with the letter following it in the alphabet (ie. c becomes d, z becomes a)"
i'm thinking of the following methods:
declare a string called "abcdefghijklmnopqrstuvxyz" and compare each string's char index position with the alphabet's index position, and then just bring the alphabet char that is located at the i+1 index location. But I don't know how it would work from z to a.
I've seen some techniques using ASCII values for every char but I've never done that before and not sure how it works
convert the given string into a char[] array, but then I'm not sure how I would tell the system to get me the next alphabet char
What would be the easiest way to do this?
EDIT
this is my code so far, but it doesn't work.
import java.util.*;
import java.io.*;
class Main {
public static String LetterChanges(String str) {
// code goes here
String alphabet = "abcdefghijklmnopqrstuvwxyz";
String newWord = "";
for (int i = 0; i < str.length(); i++){
for (int j = 0; j < alphabet.length(); i++){
if (str[i] == alphabet[i]){
if (alphabet[i+1].isVowel()){
newWord = newWord + toUpperCase(alphabet[i+1]);
}
else{
newWord = newWord + alphabet[i+1];
}
}
}
}
return str;
}
public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
System.out.print(LetterChanges(s.nextLine()));
}
}
Can't I ask for the index position of a Char that is a part of a String? in C I could do that.
Other than that not sure why it doesn't work.
I would definitely go with method 1.
I believe what you're looking for is the indexOf method on a String.
First of, I would create a method that given a character finds the next letter in the alphabet and return that. This could be done by finding the letter in your alphabet string and then fetch the letter at index+1. As you also pointed out you would need to take care of the edge case to turn 'z' into 'a', could by done with an if-statement or by having an extra letter 'a' at the end of your alphabet string.
Now all that remains to do is create a loop that runs over all characters in the message and calls the previously made method on that character and constuct a new string with the output.
Hope this helps you figure out a solution.
Assuming that there would be only lower case English letters in the given String the most performant way would be to add +1 to every character, and use either if-statement checking whethe the initial character was z or use the modulo operator % as #sp00m has pointed out in the comment.
Performing a search in the alphabetic string (option 1 in your list) is redundant, as well extracting array char[] from the given string (option 3).
Checking the edge case:
public static String shiftLetters(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char next = str.charAt(i);
if (next == 'z') result.append('a'); // checking the edge case
else result.append((char) (next + 1));
}
return result.toString();
}
Applying modulo operator:
public static String shiftLetters(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
char next = (char) ((str.charAt(i) - 'a' + 1) % 26 + 'a');
result.append(next);
}
return result.toString();
}
main()
public static void main(String[] args) {
System.out.println(shiftLetters("abc"));
System.out.println(shiftLetters("wxyz"));
}
Output:
bcd // "abc"
xyza // "wxyz"

Building up a string non-sequentially?

For example:
You're given a word and Set of letters. If the word contains letters that are not within the Set, then those letters are "replaced" with dashes. You're not actually supposed to change the given word, but create a new String that reflects those changes.
Say you were given a word: "magikarp". The Set contains the letters 'm', 'k', 'p'. The String that you would return would be "m---k--p".
How would you accomplish this by utilizing only a String and String methods? I also can't use any external libraries.
It is more intuitive to me to use arrays, but this has to be performed by building up a String instead of building any extra data structures for the sake of efficiency.
This is how I approached it (working solution but not done by building up a String), for further clarification:
public String getPattern (SortedSet<Character> guesses, String word) {
char[] pattern = word.toCharArray();
for (int i = 0; i < wordLength; i++) {
if (!guesses.contains(pattern[i])) {
pattern[i] = '-';
}
}
// Pads each character in the generated String with spaces and trims
// the leading and trailing spaces.
return new String(pattern).replace("", "").trim();
}
}
You can just use what String already provides, a method to get each character and a method to replace a specifc character with another:
public String getPattern (Set<Character> guesses, String word) {
for (int i=0; i<word.length(); ++i) {
char c = word.charAt(i);
if (!guesses.contains(c))
word = word.replace(c, '-');
}
return word;
}
Its not very efficient because it will create a new string instance for every character that needs to be replaced. For efficiency using a StringBuilder would be better.
How about building a regex String from the Set of characters, and using that as a param to the String.replaceAll method to return the filtered String?
Set<Character> letters = new HashSet<>();
letters.add('m');
letters.add('k');
letters.add('p');
String filter = "[^(";
for (Character letter : letters) {
filter += letter;
}
filter += ")]";
String toBeReplaced = "magikarp";
String result = toBeReplaced.replaceAll(filter, "-");
This is most certainly not the best way to approach the problem, except that it "builds up the String".
String newWord = "";
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
newWord = newWord + (
guesses.contains(c) ? c : '-'
);
}
This approach creates a new String instance on each letter rather than doing a quick replace. But it "builds it up".

Uppercase or Lowercase a specific character in a word "Java"

I have look into most of questions but I couldn't find how to uppercase or lowercase specific character inside a word.
Example:
String name = "Robert"
What if I would like to make "b" Uppercase and rest lowercase also how to make first letter Uppercase and rest lowercase?
Like "john" >> Output >> "John"...
I have toUppercase() and toLowercase(). They convert the whole text.
Also I tried to include charAt but never worked with me.
You will need to take your string, take a substring of the specific character or characters you want to capitalize or lowercase, and then build a new string off of it.
Example
String test = "JoHn"; //make the H lowercase
test = test.substring(0,2) + test.substring(2,3).toLowercase() + test.substring(3);
The first substring gets all characters before the desired point, the second gets the desired character and lowercases it, and the final substring gets the rest of the string
You can use toCharArray() to capitalize the first letter like this:
String name = "robert";
// Convert String to char array.
char[] arr = name.toCharArray();
// Modify first element in array.
arr[0] = Character.toUpperCase(arr[0]);
String str = new String(arr);
System.out.println(str);
Output:
Robert
And you want to make "b" Uppercase and rest lowercase like this:
// Convert String to char array.
char[] arr2 = name.toCharArray();
// Modify the third element in array.
arr2[2] = Character.toUpperCase(arr2[2]);
String str2 = new String(arr2);
System.out.println(str2);
Output:
roBert
//Try this...
String str = "Robert";
for (int i = 0; i < str.length(); i++) {
int aChar = str.charAt(i);
// you can directly use character instead of ascii codes
if (aChar == 'b') {
aChar = aChar - 32;
} else if (aChar >= 'A' && aChar <= 'Z') {
aChar += 32 ;
}
System.out.print((char) aChar);
}
/*
Output will be- roBert
*/
I wouldn't use 'test.substring(2, 3).toLowerCase()' necessarily. 'Character.valueOf(test.charAt(2)).toUpperCase()' works. Also, the 'test.substring(0, 3)' is wrong; it should be 'test.substring(0, 2)'.
A function that capitalize the first letter
private String capitalize(String str) {
return Character.toUpperCase(str.charAt(0)) + str.substring(1);
}
A function that capitalize an arbitrary letter
private String replaceCharWithUpperCase(char letterToCapitalize, String str)
{
return str.replaceAll(letterToCapitalize, Character.toUpperCase(letterToCapitalize));
}
Then you can use the previous functions like that :
String a = "JOHN";
a = capitalize(a.toLowerCase());
// now a = John.
String b = "ROBERT";
a = replaceCharWithUpperCase('b', a.toLowerCase());
// now a = roBert.

Java String. Replace list of chars by other chars list

I have String variable with value- f.e.:
this is test-str-ing_łóśżćń.
And I would like replace this chars:
, -, ł,ó,ś,ż,ć,ń
with those:
_,_,l,o,s,z,c,n.
And I mean here, that if parser will found f.e.: char - (which is second in first list) should be replaced with char that is in the same position/place in second list, which in this example is: _.
The char ó should be replaced with char o.
The char ń should be replaced with char n.
In my case the list of characters to replace is quite long and parsing in loop for each char to replace would not be enought efficient.
I know method replaceAll(). but it only accept one in String and one out String
So I am looking for method, that will allow me to work on arrays/list of Strings instead of single String.
Please give me some help.
Use java.text.Normalizer to Decompose accented letters in base letter plus "combining diacritical marks."
String base = Normalizer.normalize(accented, Form.NFKD)
.replaceAll("\\p{M}", "");
This does a decompose (D) normalization, and then removes Marks.
Some replacements still needed.
char[] out = new char[src.length()];
for( j ...){
inputChar = src.charAt(j);
for (int i = 0; i < convertChars.length; i++) {
if (inputChar == convertChars[i]) {
inputChar = toChars[i];
}
}
}
out[j] = inputChar ;
}
out2 = new String(out);
Extracted from bigger code without IDE, not tested. Loop (I hope) don't allocate objects and should not degrade speed.
Make a static lookup table:
private static char[] substitutions = new char[65536];
static {
// Initialize
for (char c = 0; c < substitutions.length; c++) {
substitutions[c] = c;
}
// Now add mappings.
substitions['-'] = '_'; // Map source->target character
... // Add the rest
}
// LATER IN Code
char[] stringChars = inputString.toCharArray();
for (int i = 0; i < stringChars.length; i++) {
stringChars[i] = substitutions[stringChars[i]];
}
outputString = new String(stringChars);

Need to use Scanner to read a file but don't know how to compare

I am trying to write a method that reads a text file using Scanner then compare them to see if they are characters ('a' - 'z') however binary operators can't be used (compilation error). Any ideas how to work around it?
I need to convert uppercase letters to lowercase, and I have a counter that keep track of how many times each letter appeared in the text file.
I also need to ignore any symbols and numbers in the text file.
After reading your comments, I changed my code into:
import java.util.Scanner;
public class LetterInventory {
int counter = 0;
private int[] inventory;
char[] alphabet = "abcdefghijklmnopqrstuvwxyz".toCharArray();
public LetterInventory () {
inventory = new int[26];
}
public void countOccurrences(Scanner file) {
while (file.hasNextLine()) {
// Read line by line and make it lowercase
String line = file.nextLine().toLowerCase();
// get all the character of the line
for (char c :line.toCharArray()) {
if (c >= 'a' && c <= 'z'){ // Check for character only
counter++;
}
}
}
}
public void displayTable () {
for (int i = 0; i < alphabet.length; i++) {
System.out.println(alphabet[i] + ": " + inventory[i]);
}
}
public void resetInventory () {
counter = 0;
}
I am still not really sure how to make this thing work.
This program is supposed to be able to read a text file, make a count each alphabet was read, ignore any symbol/number, and output a table with each letter followed by how many times they are in the text file.
As pointed out in the comments there are some problems with your code.
First: Every time you call file.next() it will try to read the next character. So what you do in your loop is: read all characters, convert to lower case but ignore this new value and carry on.
The compilation problem is due to the fact that you try to compare a string with a character.
What you want to do is something like this:
while(file.hasNext())
{
String currentTokes = file.next().toLowerCase(); //save the current token as lower text in the variable
//check each character of the string
for(char c : currentToken.toCharArray())
{
if(c <= ....) //check etc.
}
}
Another way would be to use regular expressions.
Instead of comparing file.next() to a char why not just use a regular expression?
For example:
if(file.next().matches("[a-z]")){
//do something
}
will return true if the next value picked up by the next method is a lower case character between a and z. This way you don't have to deal with unnecessary logic or worry about whether you are comparing a String to a char.
note:
I am not sure what your input is however and the above regex will only match if it is a single lower case letter not if it is a word. That said if you are reading in words you will need to split them into a character array before using the above solution.
For an example, you could try something like this:
while (file.hasNext()) {
// grabs next word/string from file
String str = file.next().toLowerCase();
// converts that string to a character array
char[] charArray = str.toCharArray();
for (char chars : charArray) {
// converts current character into a string and checks whether
// it
// is a lower case letter
if (String.valueOf(chars).matches("[a-z]")) {
// do something
}
}
}
this will work
public void countOccurrences(Scanner file) {
int[] alpha = new int[25];
while (file.hasNext()) {
char[] stringTokens = file.next().toLowerCase().toCharArray();
for (char c : stringTokens) {
if (c - 'a' > -1 || c - 'a' < 26) {
alpha[c - 'a']++;
}
}
}
}
Read inline comments for more info.
while (file.hasNextLine()) {
// Read line by line and make it lowercase
String line = file.nextLine().toLowerCase();
// get all the character of the line
for (char c : line.toCharArray()) {
if (c >= 'a' && c <= 'z'){ // check for character only
inventory[c - 'a']++; // Increment the occurrence of the Character
}
}
}

Categories

Resources