TRENDING NEWS

POPULAR NEWS

How Do I Retrieve A Second Word In A String Variable Without Using Split In Java

How do I reverse the words in a String without reversing each character and not using builtin functions like split and substring in C#?

You can use extra space to solve this:1: len= length of the original String S1.2: Create new String S2 with length= len.3: Iterate over S1 and extract each word and copy it starting from end of S2.4: Finally copy S2 to S1.ExampleAfter 1st iteration:S1: "This is a string"S2: "            This"After 2nd iteration:S1: "This is a string"S2: "         is This"After 3rd iteration:S1: "This is a string"S2: "       a is This"After 4th iteration:S1: "This is a string"S2: "string a is This"Finally copy S2 to S1.S1: "string a is This"Normally this question is asked in interviews where you cannot make use of extra space.This is how you would solve this.Step 1. Reverse each word. Step 2. Reverse the whole string.Example:Input String:    This is a stringAfter Step 1:    sihT si a gnirtsAfter Step 2:    string a is Thispublic class ReverseWords {

public static void main(String[] args) {
char[] str = "This is a string".toCharArray();
getReverse(str);
System.out.println(str);
}

public static void getReverse(char[] str) {
int n = str.length;
int start = 0;
for(int i = 0; i < n; i++) {
if(str[i] == ' ' && i > 0) {
reverse(str, start, i-1);
start = i+1;
} else if(i == n-1) {
reverse(str, start, i);
}
}
reverse(str, 0, n-1);
}

private static void reverse(char[] str, int start, int end) {
while(start < end) {
swap(str, start, end);
start++;
end--;
}
}

private static void swap(char[] str, int start, int end) {
char tmp = str[start];
str[start] = str[end];
str[end] = tmp;
}

}
Credits: Reverse words in a stringDo check out the algorithm visualization on the webpage, its pretty cool to see algorithm being animated as and when the code is getting executed.Similar question is asked here How to reverse a string without using a reverse function?  Whats the best way to reverse a string of any length word by word in BASIC.  Not using an inbuilt reverse function. For example 'Hello how are you,  Quora is awesome'  would become 'awesome is Quora you, are how Hello'  Any help appreciated  I need this for my computing A Level.  Thanks?

JAVA: Assign Word in a string to variable?

okay im doing homework and the question is:
Assume that there is another variable declared, secondWord , also of type String. Write the statements needed so that the second word of the value of sentence is assigned to secondWord . So, if the value of sentence were "Broccoli is delicious." your code would assign the value "is" to secondWord .

what i wanna know is how you will assign the second word to "secondWord"
im assuming that you use .indexOf(" ") but i cant figure it out
can someone explain this

How can we swap two strings without using a third string in Java?

It is possible to swap two strings without using third string. There are so many ways to do this one of them is mentioned below:class StringSwap
{
public static void main (String[] args) throws java.lang.Exception
{
String str1="hello";
String str2="world";
System.out.println("before swapping\nstr1 = "+str1);
System.out.println("str2 = "+str2);
str1=str2+str1;
str2=str1.replace(str2,"");
str1=str1.replace(str2,"");
System.out.println("after swapping\nstr1 = "+str1);
System.out.println("str2 = "+str2);
}
}


output:
str1= hello
str2= world
after swapping
str1= world
str2= hello
Hope you got it. All the best and keep coding..:)

How do split the variables and strings seprately in out.println() method arguments?

To print the strings use “” and follow the usual method to print variables. The following examples will help -System.out.print(" " + unit[h] + " " + "Hundred"); //unit[h] is a integer (refers to an element in an integer array)String s=”Learning”; System.out.println(“Quora is meant for “+s);I hope this will help you!

What is the simplest way to get the last two word of a string in Java?

first of all, determine the length of the string by the function length() which has to be called using the string variable. then print the last and second last values as showed in following code#includestring s;int n= s.length(); // this will give size of stringprintf(“%d”, s[n-2]); //for second last characterprintf(“%d”, s[n-1]); //for last characterdon’t forget to upvote

What is the simplest way to get the last word of a string in Java? You can assume no punctuation (just alphabetic characters and whitespace).

I have tried this code and it worked for me. Hope you guys get result :)import java.util.*;class LastString {public static void main(String args[]){String string = "";Scanner in = new Scanner(System.in);System.out.println("Enter a sentence : ");string= in.nextLine();System.out.println("Last word of the sentence is : " + string.substring (string.lastIndexOf (' '), string.length()));}}

How do I find the word in a string containing all the vowels in Java?

Here are couple of ways to do that. The first uses the imperative style and the second uses the functional-style coding.The following code is common for both styles of programming:List vowels = Arrays.asList('a', 'e', 'i', 'o', 'u');
String str = "A vowels string with cauliflower and education";
String [] words = str.split(" ");
The variable vowels defines all the vowels as characters in a collection. The variable str is the input string and it has two words which has all the vowels: “cauliflower” and ”education”.Imperative-style CodeList resultWords = new ArrayList<>();

OUTER_LOOP:
for (String word : words) {
for (char vw: vowels) {
if (word.indexOf(vw) == -1) {
// discard this word and go for the next word
continue OUTER_LOOP;
}
}

resultWords.add(word);
}

System.out.println(resultWords);
The output: [cauliflower, education]Functional-style CodeArrays.stream(words)
.filter(word -> vowels.stream()
.allMatch(vw -> word.indexOf(vw) != -1))
.forEach(System.out::println);
The output:cauliflowereducation

Accept a word then print every letter of that word on new line in java?

here is my code

import java.util.Scanner;
public class exercise_4
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
int a;
a = 0;
System.out.println("Please enter your words");

String word = scan.nextLine();

System.out.println(word.charAt(a));

}
}

C# string split method?

If you're using cin, then you will only get the first term from the sentence, once you get through that, you can use string.substr(0,string.first(" ")) and then loop through until string.first(" "); returns false.

TRENDING NEWS