Solution in Java :
import java.util.*;
public class ReverseWords{
public static void main(String[] args){
System.out.print("Enter the string: ");
Scanner input=new Scanner(System.in);
String str=input.nextLine();
StringBuffer buffer = new StringBuffer(str);
StringTokenizer st = new StringTokenizer(buffer.reverse().toString(), " ");
System.out.print("Reversed Words: ");
while(st.hasMoreTokens()){
StringBuffer sb= new StringBuffer(st.nextToken());
System.out.print(" "+sb.reverse());
}
}
}
References :
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the
The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.
An instance of
A token is returned by taking a substring of the string that was used to create the StringTokenizer object.
The following is one example of the use of the tokenizer. The code:
The following example illustrates how the String.split method can be used to break up a string into its basic tokens:
String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
The principal operations on a
For example, if
In general, if sb refers to an instance of a
Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread,
Solution in C :
void ReverseWords(char *str)
{
int start, end, length;
length=strlen(str);
ReverseString(str, 0, length-1);
start=end=0;
while (end < length)
{
if (str[end] ! = ' ')
{
start=end;
while (str[end] != ' ' && end < length)
end++;
end--;
ReverseString(str, start, end);
}
end++;
}
}
void ReverseString(str, start, end)
{
char *temp;
while (start < end)
{
temp=str[start];
str[start]=str[end];
str[end]=temp;
start++;
end--;
}
}
import java.util.*;
public class ReverseWords{
public static void main(String[] args){
System.out.print("Enter the string: ");
Scanner input=new Scanner(System.in);
String str=input.nextLine();
StringBuffer buffer = new StringBuffer(str);
StringTokenizer st = new StringTokenizer(buffer.reverse().toString(), " ");
System.out.print("Reversed Words: ");
while(st.hasMoreTokens()){
StringBuffer sb= new StringBuffer(st.nextToken());
System.out.print(" "+sb.reverse());
}
}
}
References :
The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the
StreamTokenizer
class. The StringTokenizer
methods do not distinguish among identifiers, numbers, and quoted strings, nor do they recognize and skip comments. The set of delimiters (the characters that separate tokens) may be specified either at creation time or on a per-token basis.
An instance of
StringTokenizer
behaves in one of two ways, depending on whether it was created with the returnDelims
flag having the value true
or false
: - If the flag is
false
, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters. - If the flag is
true
, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character, or a maximal sequence of consecutive characters that are not delimiters.
A token is returned by taking a substring of the string that was used to create the StringTokenizer object.
The following is one example of the use of the tokenizer. The code:
prints the following output:StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); }
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.this is a test
The following example illustrates how the String.split method can be used to break up a string into its basic tokens:
prints the following output:String[] result = "this is a test".split("\\s"); for (int x=0; x<result.length; x++) System.out.println(result[x]);
A thread-safe, mutable sequence of characters. A string buffer is like athis is a test
String
, but can be modified. At any point in time it contains some particular sequence of characters, but the length and content of the sequence can be changed through certain method calls. String buffers are safe for use by multiple threads. The methods are synchronized where necessary so that all the operations on any particular instance behave as if they occur in some serial order that is consistent with the order of the method calls made by each of the individual threads involved.
The principal operations on a
StringBuffer
are the append
and insert
methods, which are overloaded so as to accept data of any type. Each effectively converts a given datum to a string and then appends or inserts the characters of that string to the string buffer. The append
method always adds these characters at the end of the buffer; the insert
method adds the characters at a specified point. For example, if
z
refers to a string buffer object whose current contents are "start
", then the method call z.append("le")
would cause the string buffer to contain "startle
", whereas z.insert(4, "le")
would alter the string buffer to contain "starlet
". In general, if sb refers to an instance of a
StringBuffer
, then sb.append(x)
has the same effect as sb.insert(sb.length(), x)
. Whenever an operation occurs involving a source sequence (such as appending or inserting from a source sequence) this class synchronizes only on the string buffer performing the operation, not on the source.
Every string buffer has a capacity. As long as the length of the character sequence contained in the string buffer does not exceed the capacity, it is not necessary to allocate a new internal buffer array. If the internal buffer overflows, it is automatically made larger. As of release JDK 5, this class has been supplemented with an equivalent class designed for use by a single thread,
StringBuilder
. The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.Solution in C :
void ReverseWords(char *str)
{
int start, end, length;
length=strlen(str);
ReverseString(str, 0, length-1);
start=end=0;
while (end < length)
{
if (str[end] ! = ' ')
{
start=end;
while (str[end] != ' ' && end < length)
end++;
end--;
ReverseString(str, start, end);
}
end++;
}
}
void ReverseString(str, start, end)
{
char *temp;
while (start < end)
{
temp=str[start];
str[start]=str[end];
str[end]=temp;
start++;
end--;
}
}
No comments:
Post a Comment