I would like to have this code be able to locate more than one X and output it as Location1, location2,.......
i.e input: xuyx
i would have it output 0,3
import java.util.Scanner;
public class findinline {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String str;
str = input.nextLine();
int pos = str.indexOf("x");
if (pos < 0){
System.out.println("No X Detected");
}
else{
System.out.println(pos);
}
}
}
String has the method indexOf(String str, int fromIndex) http://docs.oracle.com/javase/7/docs/api/java/lang/String.html
So just increment the starting index to where you found the last one.
String xStr = "xuyx";
int index = xStr.indexOf("x", 0);
while(index >= 0)
{
System.out.println(index);
index = xStr.indexOf("x", index + 1);
}
or better yet...
public List<Integer> getIndexesOfStr(String fullStr, String strToFind){
ArrayList<Integer> listOfIndexes = new ArrayList<>();
int index = fullStr.indexOf(strToFind, 0);
while(index >= 0)
{
listOfIndexes.add(index);
index = fullStr.indexOf(strToFind, index + strToFind.length());
}
return listOfIndexes;
}
You could do it like this. Iterate through the characters and print out the indexes where you hit an x.
boolean any = false;
for (int pos = 0; pos < str.length(); ++pos) {
if (str.charAt(pos)=='x') {
if (any) {
System.out.print(',');
}
any = true;
System.out.print(pos);
}
}
if (!any) {
System.out.println("No x found");
} else {
System.out.println();
}
Bear in mind you'd have to fix the case if you want to detect capital X as well.
Or, as kingdamian42 says, you could use indexOf(txt, fromindex)
Related
I have a string s to which I want to append another string s1 at the specified position.
String s = "17.4755,2.0585,23.6489,12.0045";
String s1=",,,,"
Now I want to add the string s1 after the n-th occurrence of "," character.
I have just started learning Java.
You can use the following method:
public String insert(int n, String original, String other) {
int index = original.indexOf(',');
while(--n > 0 && index != -1) {
index = original.indexOf(',', index + 1);
}
if(index == -1) {
return original;
} else {
return original.substring(0, index) + other + original.substring(index);
}
}
Working with Strings directly is not worth the trouble.
One easy way would be to turn your String into a List and manipulate that.
public void test() {
String s = "17.4755,2.0585,23.6489,12.0045";
// Split s into parts.
String[] parts = s.split(",");
// Convert it to a list so we can insert.
List<String> list = new ArrayList<>(Arrays.asList(parts));
// Inset 3 blank fields at position 2.
for (int i = 0; i < 3; i++) {
list.add(2,"");
}
// Create my new string.
String changed = list.stream().collect(Collectors.joining(","));
System.out.println(changed);
}
Prints:
17.4755,2.0585,,,,23.6489,12.0045
I think this is what you want
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = "17.4755,2.0585,23.6489,12.0045";
String s1=",,,,";
System.out.println("Enter Nth Occurrence");
try {
int n = scanner.nextInt();
long totalOccurrence = 0;
if (n != 0) {
totalOccurrence = s.chars().filter(num -> num == ',').count();
if (totalOccurrence < n) {
System.out.println("String s have only " + totalOccurrence + " symbol \",\"");
} else {
int count = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ',') {
count++;
if (count == n) {
String resultString = s.substring(0, i) + s1 + s.substring(i, s.length());
System.out.println(resultString);
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("Wrong input");
}
}
}
Output :
1. Enter Nth Occurrence
5
String s have only 3 symbol ","
2. Enter Nth Occurrence
2
17.4755,2.0585,,,,,23.6489,12.0045
I have a large string like " ali li vali bali", and I want to know that how many times all words(I mean individual words like li) are repeated (ali, li, vali and bali are only considered as substrings) apart from its own existence.
For example: li is substring in a large string and it is repeating thrice other than its own existence.
li in ali, li in vali and li in bali. count is: 3.
This is my code and the error I got:
import java.util.*;
public class main{
public static void main(String args[]){
String str="";
Scanner scan= new Scanner(System.in);
while(scan.hasNext())
{
str+= scan.nextLine();
}
int n= str.trim().split("\\s+").length;
String[] wordsArray=str.split(" ");
substring sub=new substring();
for(int i=0;i<wordsArray.length;i++){
int count=sub.sub(wordsArray[i],str);
csub+=count;
}
System.out.println("number of substring: "+csub);
and substring do:
public class substring{
public int sub(String str,String str2){
String[] wordsArray=str2.split(" ");
int len=str.length();
int b=0;
String[] str1=new String[len*2+2];
int k=0;
for (int from = 0; from < str.length(); from++) {
for (int to = from + 1; to <= str.length(); to++) {
str1[k]=str.substring(from, to);
k++;
}
}
int index = 0;
int count = 0;
for(int i=0;i<str1.length;i++){
if(str1[i].length()>2){
while ((index = str2.indexOf(str1[i], index)) != -1) {
index += str1[i].length();
count++;
b++;
}
}
}
return b;
}
}
What is wrong?
**I got this error:**
Expection in thread "main" java lang.NullPointerExpection
at substring.sub<substring.java:20>
at main.main<main.java:52>
You can just scan the string without splitting it.
Example:
public static int countSubstring(String str, String substring) {
// start at the beggining of the string
int pos = 0;
int count = 0;
// search starting at pos and store the index (if found) on pos
while ((pos = str.indexOf(substring, pos)) != -1) {
count++;
// pos is at the index of last found substring
// advance it by the length of the substring
pos = pos + substring.length();
}
return count;
}
public static void main(String[] args) {
System.out.println(countSubstring("li, vali and bali", "li"));
}
Output
3
You could do it like this,
public static int sub(String str, String str2) {
if (str == null || str2 == null) {
return 0;
}
int count = 0;
int p = str2.indexOf(str);
while (p >= 0) {
count++;
p = str2.indexOf(str, p + str.length());
}
return count;
}
public static void main(String[] args) throws Exception {
String toSearch = "Hi Dee Hi";
System.out.println(sub("Hi", toSearch));
System.out.println(sub("Dee", toSearch));
System.out.println(sub("Ho", toSearch));
}
Outputs
2
1
0
This question already has answers here:
Printing reverse of any String without using any predefined function?
(34 answers)
Closed 8 years ago.
I was asked this in a technical interview. I have no idea whatsoever please please help me.
it goes in infinite loop. I just cant find the correct logic.
not once, but twice i came across this kind of a question, so please help
public static int numberOfCharsInString(String sentence)
{
int numberOfChars = 0,i=0;
while (!sentence.equals(""))
{
sentence = sentence.substring(1);
++numberOfChars;
}
return numberOfChars;
}
public static void reverseSequenceOfWords(String inp)
{
int len=numberOfCharsInString(inp);
char[] in=inp.toCharArray();
int i=0;
for(i=len-1;i>=0;i--)
{
if(in[i]==' ')
{
while(!in.equals("")||in.equals(" "))
{
System.out.print(in[i]+" ");
}
}
else if(in[i]=='\0')
{
break;
}
}
}
public static void main(String[] args)
{
int length=0;
String inpstring = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
try
{
System.out.print("Enter a string to reverse:");
inpstring = reader.readLine();
length=numberOfCharsInString(inpstring);
System.out.println("Number of Characters: "+length);
reverseSequenceOfWords(inpstring);
}
catch (Exception e)
{
e.printStackTrace();
}
}
String[] array = "Are you crazy".split(" ");
for (int i = array.length - 1; i >= 0; --i) {
System.out.print(array[i] + " ");
}
Brute forced this so hard lol
public static void main (String args[]){
String input = new Scanner(System.in).nextLine();
input+=" ";
ArrayList<String> words = new ArrayList<String>();
int start = 0;
for(int i=0; i<input.length(); i++){
if(input.charAt(i)==' '){
String toAdd="";
for(int r=start; r<i; r++){
toAdd+=input.charAt(r);
}
words.add(toAdd);
start = i+1;
}
}
for(int i=words.size()-1; i>=0; i--){
System.out.print(words.get(i)+" ");
}
}
I've used String.length() and String.substring()and String.charAt() - I hope that is allowed.
private static class Word {
private final String message;
private final int start;
private final int end;
public Word(String message, int start, int end) {
this.message = message;
this.start = start;
this.end = end;
}
#Override
public String toString() {
return message.substring(start, end);
}
}
private Word[] split(String message) {
// Split it into words - there cannot be more words than characters in the message.
int[] spaces = new int[message.length()];
// How many words.
int nWords = 0;
// Pretend there's a space at the start.
spaces[0] = -1;
// Walk the message.
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == ' ') {
spaces[++nWords] = i;
}
}
// Record the final position.
spaces[++nWords] = message.length();
// Build the word array.
Word[] words = new Word[nWords];
for (int i = 0; i < nWords; i++) {
words[i] = new Word(message, spaces[i] + 1, spaces[i + 1]);
}
return words;
}
private String reverse(String message) {
Word[] split = split(message);
String reversed = "";
for ( int i = split.length - 1; i >= 0; i--) {
reversed += split[i].toString();
if ( i > 0 ) {
reversed += " ";
}
}
return reversed;
}
public void test() {
String message = "Hello how are you today?";
System.out.println(reverse(message));
}
prints
today? you are how Hello
Much more minimal but less useful. Only uses length, charAt and substring again:
public void printWordsReversed(String message) {
int end = message.length();
for ( int i = end - 1; i >= 0; i--) {
if ( message.charAt(i) == ' ') {
System.out.print(message.substring(i+1, end)+" ");
end = i;
}
}
System.out.print(message.substring(0, end));
}
The only function i'm still using is the IndexOf function, but that is not that hard to create for yourself.
static void Main(string[] args)
{
string sentence = "are you cracy";
int length = Program.StringLength(sentence);
int currentpos = 0;
List<string> wordList = new List<string>();
int wordCount = 0;
while (currentpos < length)
{
// find the next space
int spacepos = sentence.IndexOf(' ', currentpos);
string word;
if (spacepos < 0)
{
// end of string reached.
word = sentence.Substring(currentpos, length - currentpos);
wordList.Add(word);
wordCount++;
// no need to continue.
break;
}
word = sentence.Substring(currentpos, spacepos - currentpos);
wordList.Add(word);
wordCount++;
currentpos = spacepos + 1;
}
// display
for (int i = wordList.Count - 1; i >= 0; i--)
{
// after first word is display, add spaces to the output
if (i < wordList.Count - 1)
{
Console.WriteLine(" ");
}
// display word
Console.WriteLine(wordList[i]);
}
}
public static int StringLength(String sentence)
{
int numberOfChars = 0;
while (!sentence.Equals(""))
{
sentence = sentence.Substring(1);
++numberOfChars;
}
return numberOfChars;
}
My goal is to write a program that compresses a string, for example:
input: hellooopppppp!
output:he2l3o6p!
Here is the code I have so far, but there are errors.
When I have the input: hellooo
my code outputs: hel2l3o
instead of: he213o
the 2 is being printed in the wrong spot, but I cannot figure out how to fix this.
Also, with an input of: hello
my code outputs: hel2l
instead of: he2lo
It skips the last letter in this case all together, and the 2 is also in the wrong place, an error from my first example.
Any help is much appreciated. Thanks so much!
public class compressionTime
{
public static void main(String [] args)
{
System.out.println ("Enter a string");
//read in user input
String userString = IO.readString();
//store length of string
int length = userString.length();
System.out.println(length);
int count;
String result = "";
for (int i=1; i<=length; i++)
{
char a = userString.charAt(i-1);
count = 1;
if (i-2 >= 0)
{
while (i<=length && userString.charAt(i-1) == userString.charAt(i-2))
{
count++;
i++;
}
System.out.print(count);
}
if (count==1)
result = result.concat(Character.toString(a));
else
result = result.concat(Integer.toString(count).concat(Character.toString(a)));
}
IO.outputStringAnswer(result);
}
}
I would
count from 0 as that is how indexes work in Java. Your code will be simpler.
would compare the current char to the next one. This will avoid printing the first character.
wouldn't compress ll as 2l as it is no smaller. Only sequences of at least 3 will help.
try to detect if a number 3 to 9 has been used and at least print an error.
use the debugger to step through the code to understand what it is doing and why it doesn't do what you think it should.
I am doing it this way. Very simple:
public static void compressString (String string) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < string.length(); i++) {
int count = 1;
while (i + 1 < string.length()
&& string.charAt(i) == string.charAt(i + 1)) {
count++;
i++;
}
if (count > 1) {
stringBuffer.append(count);
}
stringBuffer.append(string.charAt(i));
}
System.out.println("Compressed string: " + stringBuffer);
}
You can accomplish this using a nested for loops and do something simial to:
count = 0;
String results = "";
for(int i=0;i<userString.length();){
char begin = userString.charAt(i);
//System.out.println("begin is: "+begin);
for(int j=i+1; j<userString.length();j++){
char next = userString.charAt(j);
//System.out.println("next is: "+next);
if(begin == next){
count++;
}
else{
System.out.println("Breaking");
break;
}
}
i+= count+1;
if(count>0){
String add = begin + "";
int tempcount = count +1;
results+= tempcount + add;
}
else{
results+= begin;
}
count=0;
}
System.out.println(results);
I tested this output with Hello and the result was He2lo
also tested with hellooopppppp result he2l3o6p
If you don't understand how this works, you should learn regular expressions.
public String rleEncodeString(String in) {
StringBuilder out = new StringBuilder();
Pattern p = Pattern.compile("((\\w)\\2*)");
Matcher m = p.matcher(in);
while(m.find()) {
if(m.group(1).length() > 1) {
out.append(m.group(1).length());
}
out.append(m.group(2));
}
return out.toString();
}
Try something like this:
public static void main(String[] args) {
System.out.println("Enter a string:");
Scanner IO = new Scanner(System.in);
// read in user input
String userString = IO.nextLine() + "-";
int length = userString.length();
int count = 0;
String result = "";
char new_char;
for (int i = 0; i < length; i++) {
new_char = userString.charAt(i);
count++;
if (new_char != userString.charAt(i + 1)) {
if (count != 1) {
result = result.concat(Integer.toString(count + 1));
}
result = result.concat(Character.toString(new_char));
count = 0;
}
if (userString.charAt(i + 1) == '-')
break;
}
System.out.println(result);
}
The problem is that your code checks if the previous letter, not the next, is the same as the current.
Your for loops basically goes through each letter in the string, and if it is the same as the previous letter, it figures out how many of that letter there is and puts that number into the result string. However, for a word like "hello", it will check 'e' and 'l' (and notice that they are preceded by 'h' and 'e', receptively) and think that there is no repeat. It will then get to the next 'l', and then see that it is the same as the previous letter. It will put '2' in the result, but too late, resulting in "hel2l" instead of "he2lo".
To clean up and fix your code, I recommend the following to replace your for loop:
int count = 1;
String result = "";
for(int i=0;i<length;i++) {
if(i < userString.length()-1 && userString.charAt(i) == userString.charAt(i+1))
count++;
else {
if(count == 1)
result += userString.charAt(i);
else {
result = result + count + userString.charAt(i);
count = 1;
}
}
}
Comment if you need me to explain some of the changes. Some are necessary, others optional.
Here is the solution for the problem with better time complexity:
public static void compressString (String string) {
LinkedHashSet<String> charMap = new LinkedHashSet<String>();
HashMap<String, Integer> countMap = new HashMap<String, Integer>();
int count;
String key;
for (int i = 0; i < string.length(); i++) {
key = new String(string.charAt(i) + "");
charMap.add(key);
if(countMap.containsKey(key)) {
count = countMap.get(key);
countMap.put(key, count + 1);
}
else {
countMap.put(key, 1);
}
}
Iterator<String> iterator = charMap.iterator();
String resultStr = "";
while (iterator.hasNext()) {
key = iterator.next();
count = countMap.get(key);
if(count > 1) {
resultStr = resultStr + count + key;
}
else{
resultStr = resultStr + key;
}
}
System.out.println(resultStr);
}
I am trying to solve a HackerRank challenge, but I am running into a problem. Obviously the brute force solution of O(n^2) will not cut if for the performance requirements (I tried), so I began searching for a more elegant solution. That is when I landed on KMP. And following this tutorial I implemented it.
However, the challenge states that you can actually have one mismatch in the strings, so I tried to add that functionality in my code. However, I am getting results that are not correct and I am completely clueless as to why. Could someone please take a look at my solution and point me in the right direction?
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
//This failure function creates an array of integers
//that indicate if there is a prefix that is both a
//prefix and a suffix of the word and at what position
//the prefix ends
private static int[] failureFunction(char[] p){
int i = 0;
int j = -1;
int len = p.length;
int[] f = new int[len+1];
f[i] = j;
int potentialWrong = 0;
while(i<len){
// if(j>=0 && p[i] != p[j]){
// potentialWrong++;
// }
// if(potentialWrong > 1){
// potentialWrong = 0;
while(j>=0 && p[i] != p[j]){
//if there is a mismatch consider the
//next widest border. The borders to be
//examined are obtained in decreasing order
// from the values f[i], f[f[i]] etc.
j=f[j];
}
// }
i++;
j++;
f[i]=j;
}
// for(int k : f){
// System.out.print(k+" ");
// }
return f;
}
private static LinkedList<Integer> searchKMP(char[] text, char[] ptrn){
int[] b = failureFunction(ptrn);
int j=0;
int i=0;
// pattern and text lengths
int ptrnLen = ptrn.length;
int txtLen = text.length;
int potentialWrong = 0;
LinkedList<Integer> list = new LinkedList<Integer>();
while(i<txtLen){
// System.out.println("i: "+i +", j: " +j);
if(text[i] != ptrn[j]){
potentialWrong++;
}
System.out.println("txt: " +text[i] +", ptrn: "+ptrn[j]);
System.out.println("Num wrong: " + potentialWrong);
if(potentialWrong > 1){
potentialWrong = 0;
while (j >= 0 && text[i] != ptrn[j]) {
j = b[j];
}
}
i++;
j++;
// a match is found
if (j == ptrnLen) {
list.add(i - ptrnLen);
j = b[j];
}
}
return list;
}
// private static boolean isValidMatch(String maybe, String virus){
// int numWrong = 0;
// // System.out.println(maybe +"vs."+ virus);
// for(int i=0;i<maybe.length();i++){
// if(maybe.charAt(i) != virus.charAt(i)){
// numWrong++;
// }
// if(numWrong > 1 ) return false;
// }
// return true;
// }
// private static LinkedList<Integer> getMatches(String patient, String virus){
// LinkedList<Integer> indices = new LinkedList<Integer>();
// for(int i=0;i<patient.length();i++){
// if(i+virus.length()-1 < patient.length()){
// if(isValidMatch(patient.substring(i, i+virus.length()), virus)){
// indices.add(i);
// }
// }
// else{
// break;
// }
// }
// return indices;
// }
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int T = scn.nextInt();
String patient;
String virus;
for(int i =0;i<T;i++){
scn.nextLine(); //for empty line
patient = scn.nextLine();
virus = scn.nextLine();
LinkedList<Integer> list = searchKMP(patient.toCharArray(), virus.toCharArray());
for(int k : list){
System.out.print(k+" ");
}
System.out.println("");
}
}
}
public class KMPStringSearch {
/**
* Searches for all occurances of the word in the sentence. Runs in O(n+k)
* where n is the word length and k is the sentence length.
*
* #param word The word that is being searched
* #param sentence The collections of word over which the search happens.
* #return The list of starting indices of the matched word in the sentence.
* Empty list in case of no match.
*/
public List<Integer> searchString(final String word, final String sentence) {
final List<Integer> matchedIndices = new ArrayList<>();
final int sentenceLength = sentence.length();
final int wordLength = word.length();
int beginMatch = 0; // the starting position in sentence from which the match started
int idxWord = 0; // the index of the character of the word that is being compared to a character in string
final List<Integer> partialTable = createPartialMatchTable(word);
while (beginMatch + idxWord < sentenceLength) {
if (word.charAt(idxWord) == sentence.charAt(beginMatch + idxWord)) {
// the characters have matched
if (idxWord == wordLength - 1) {
// the word is complete. we have a match.
matchedIndices.add(beginMatch);
// restart the search
beginMatch = beginMatch + idxWord - partialTable.get(idxWord);
if (partialTable.get(idxWord) > -1) {
idxWord = partialTable.get(idxWord);
} else {
idxWord = 0;
}
} else {
idxWord++;
}
} else {
// mismatch. restart the search.
beginMatch = beginMatch + idxWord - partialTable.get(idxWord);
if (partialTable.get(idxWord) > -1) {
idxWord = partialTable.get(idxWord);
} else {
idxWord = 0;
}
}
}
return Collections.unmodifiableList(matchedIndices);
}
/**
* Creates the Partial Match Table for the word. Runs in O(n) where n is the
* length of the word.
*
* #param word The word whose Partial Match Table is required.
* #return The table as a list of integers.
*/
public List<Integer> createPartialMatchTable(final String word) {
if (StringUtils.isBlank(word)) {
return Collections.EMPTY_LIST;
}
final int length = word.length();
final List<Integer> partialTable = new ArrayList<>(length + 1);
partialTable.add(-1);
partialTable.add(0);
final char firstChar = word.charAt(0);
for (int idx = 1; idx < word.length(); idx++) {
final int prevVal = partialTable.get(idx);
if (prevVal == 0) {
if (word.charAt(idx) == firstChar) {
partialTable.add(1);
} else {
partialTable.add(0);
}
} else if (word.charAt(idx) == word.charAt(prevVal)) {
partialTable.add(prevVal + 1);
} else {
partialTable.add(0);
}
}
return Collections.unmodifiableList(partialTable);
}
}