Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
You are not allowed to use any inbuilt functions like indexOf(), contains() or matches() of String class.
Find string apple in string webapple using given char arrays?
String webapple ="webapple";
String apple="apple";
char[] webappleArray=webapple.toCharArray();
char[] appleArray = apple.toCharArray();
write a function
public boolean isPresent(char[] apple ,char[] webapple ){
//your code here
}
I add it here in case someone really need it or want to study from it:
public static void main(String[] args) {
String webapple = "webapple";
String apple = "apple";
char[] webappleArray = webapple.toCharArray();
char[] appleArray = apple.toCharArray();
System.out.println(isPresent(appleArray, webappleArray));
}
public static boolean isPresent(char[] apple, char[] webapple) {
for (int i = 0; i < webapple.length - apple.length+1; i++) {
for (int j = 0; j < apple.length; j++) {
if (webapple[i + j] == apple[j]) {
if (j == apple.length - 1) {
return true;
}
} else {
break;
}
}
}
return false;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
//. I was writing code to get first letters of all words in a string.
public class Firstword {
static void func(String str)
{
String k ="";
String str1=" "+str;
char[] ch= str1.toCharArray();
for(int i=0;i<ch.length-2;i++)
{
if(i != ch.length-1)
while(i<ch.length && ch[i]!=' ')
i++;
k=k+ch[i+1];
}
System.out.print(k);
System.out.print(ch.length);
}
public static void main(String[] args)
{
String str = "Hello Banner jee";
func(str);
}
}
Your error is here:
k=k+ch[i+1];
You are getting out of bounds.
Because of this:
while(i<ch.length && ch[i]!=' ')
i++;
Something like this will work -
static void func(String str)
{
String [] words = str.split(" ");
for(int i = 0; i < words.length ;i++){
System.out.println(words[i].charAt(0));
}
}
public static void main(String[] args)
{
String str = "Hello Banner jee";
func(str);
}
Output -
H
B
j
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm new for Java,and I want to know can we increase the variable value through charAt() in java as below.
public class CheckPalindrome{
public static boolean isPalindrome(String text) {
int length = text.length();
int forward = 0;
int backward = length - 1;
while (backward > forward) {
char forwardChar = text.charAt(forward++);
char backwardChar = text.charAt(backward--);
if (forwardChar != backwardChar)
return false;
}
return true;
}
public static void main(String args[]){
System.out.println (isPalindrome("level"));
}
}
I want to know what is happening below code line..
char forwardChar = text.charAt(forward++);
String is immutable, so no you can't do that. You need to create a new string (e.g. with substring) and combine the results:
String text = "ABCCEFG";
char midCharacter = text.charAt(3);
midCharacter++;
String output = text.substring(0, 3) + midCharacter + text.substring(4);
Output:
ABCDEFG
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to make a ReverseString program. It's only returning one word only. I would like a full sentence.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word = input.next();
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--)
reverse += word.charAt(i);
System.out.println(reverse);
}
}
You can use the reverse method of the StringBuilder/StringBuffer class.
Something like :
String reversedString = new StringBuilder(input.nextLine()).reverse().toString();
Or if you want a more low-level approach you could use a Stack push every character in it and pop it one by one to get the reversal of it.
public String reverseString(String s) {
Stack<Character> stack = new Stack<>();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
stack.push(s.charAt(i));
}
while (!stack.empty()) {
stringBuilder.append(stack.pop());
}
return stringBuilder.toString();
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a class like below
public class EXOR{
public static void conv(){
String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
"ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};
String binAddr[]=new String[18];
for (int i=0;i<18;i++)
{
int x[]=new int[18];
binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));
System.out.println("binernya : " +binAddr[i]);
}
}
public static void main(String[] args){
new EXOR().conv();
}
}
and I want to convert that array to binary array format.
I want to get output like below
for example
00100100001111110110101010001000
10000111101000110000100011010011
................................
How to fix this problem?
I suppose while executing your code you must've got a number point exception. This occurs when the Hexadecimal string is out of the range of Integer.
You can use:
binAddr[i]= (new BigInteger(Parray[i],16)).toString(2);
instead of
binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));
This will solve your problem for quick reference
Big Integer Documentation
Code:
public class EXOR {
public static void conv(){
String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
"ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};
String [] binAddr = new String[Parray.length];
for (int i = 0; i < binAddr.length; i++)
{
int strLen = Parray[i].length();
binAddr[i] = "";
for(int j = 0; j < strLen; j++) {
String temp = Integer.toBinaryString(
Integer.parseInt(String.valueOf(
Parray[i].charAt(j)), 16));
// Pad with leading zeroes
for(int k = 0; k < (4 - temp.length()); k++) {
binAddr[i] += "0";
}
binAddr[i] += temp;
}
System.out.println("Original: " + Parray[i]);
System.out.println("Binary: " + binAddr[i]);
}
}
public static void main(String[] args){
conv();
}
}
First few lines of Output:
Original: 243f6a88
Binary: 00100100001111110110101010001000
Original: 85a308d3
Binary: 10000101101000110000100011010011
We have Integer.MAX_VALUE = 2147483647
But, the 2nd item "85A308D3" = 2242054355. It exceed the capability of an Integer.
So, you use Integer.parseInt(85A308D3) will cause java.lang.NumberFormatException.
To fix it, change your code to use Long instead of Integer
binAddr[i] = Long.toBinaryString(Long.parseLong(Parray[i], 16));
Hope this help!
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to reverse words in string of java without using split method and StringTokenizer.
For example, How are you must be printed in you are How.
I tried but I failed to do it.
Any help will be appreciated.
Try below code snippet
import java.util.ArrayList;
public class ReverseString
{
public static void main(String args[])
{
String myName = "Here we go";
ArrayList al = new ArrayList();
al = recursiveReverseMethod(myName,al);
al.trimToSize();
StringBuilder sb = new StringBuilder();
for(int i = al.size()-1; i>=0;i--)
{
sb.append(al.get(i)+" ");
}
System.out.println(sb);
}
public static ArrayList recursiveReverseMethod(String myName,ArrayList al)
{
int index = myName.indexOf(" ");
al.add(myName.substring(0, index));
myName = myName.substring(index+1);
if(myName.indexOf(" ")==-1)
{
al.add(myName.substring(0));
return al;
}
return recursiveReverseMethod(myName,al);
}
}
Here is another flavor based on the old time logic of String reversal in 'C'., from this thread.,
class testers {
public static void main(String[] args) {
String testStr="LongString";
testers u= new testers();
u.reverseStr(testStr);
}
public void reverseStr(String testStr){
char[] d= testStr.toCharArray();
int i;
int length=d.length;
int last_pos;
last_pos=d.length-1;
for (i=0;i<length/2;i++){
char tmp=d[i];
d[i]=d[last_pos-i];
d[last_pos-i]=tmp;
}
System.out.println(d);
}
}
I would do this:
public static String reverseWordsWithoutSplit(String sentence){
if (sentence == null || sentence.isEmpty()) return sentence;
int nextSpaceIndex = 0;
int wordStartIndex = 0;
int length = sentence.length();
StringBuilder reversedSentence = new StringBuilder();
while (nextSpaceIndex > -1){
nextSpaceIndex = sentence.indexOf(' ', wordStartIndex);
if (nextSpaceIndex > -1) reversedSentence.insert(0, sentence.substring(wordStartIndex, nextSpaceIndex)).insert(0, ' ');
else reversedSentence.insert(0, sentence.subSequence(wordStartIndex, length));
wordStartIndex = nextSpaceIndex + 1;
}
return reversedSentence.toString();
}