Hi I want to print the string " my name is xxx" as "xxx is name my" with out using Special methods (like util package methods in java); thanks
Yes, but there are other string manipulation classes that live in java.util, like StringTokenizer.
R. Bemrose
2010-03-18 18:05:25
+5
A:
String text = "my name is xxx";
String reversed = "";
for (String word : text.split(" ")) {
reversed = word + " " + reversed;
}
reversed = reversed.trim();
If absolutely no method call is allowed, then it's a bit messier.
String text = "my name is xxx";
char[] letters = text.toCharArray();
char[] srettel = new char[letters.length];
for (int last = letters.length, i = last - 1, j = 0; i >= -1; i--) {
if (i == -1 || letters[i] == ' ') {
for (int k = i + 1; k < last; ) {
srettel[j++] = letters[k++];
}
if (i != -1) srettel[j++] = ' ';
last = i;
}
}
String reversed = new String(srettel);
This requires a bit more explanation. We read the input array right to left (i--
), and whenever we find a space, or when we eventually hit the left wall (i == -1
), we copy (for k
) the word we found to the output array ([j++] =
). We keep track of the word boundaries in last
.
polygenelubricants
2010-03-11 08:57:26
@Mohanavel: Technically, everything aside from the operators is Java API ...
Noon Silk
2010-03-11 09:47:10
@Helper: my philosophy is not to be judgemental and/or presumptious; I'm just here to offer the best, most instructive explanation that I can give.
polygenelubricants
2010-03-11 09:55:19
@Helper: I finally read the community-written guideline regarding homework (http://meta.stackoverflow.com/questions/10811/homework-on-stackoverflow), which I probably should've done earlier. In any case, "Don't downvote others who answer homework questions in good faith, even if they break these guidelines.". As for my part, I'll provide pseudocodes instead next time.
polygenelubricants
2010-03-11 10:45:44
I'm sorry then, didn't know these guidelines existed. Thinking about it, if this student is eager to learn, he will try to understand what you've written, and if not, he probably won't be studying for too long ;-). So in any case, it's really not that much of a problem, I apologize :-).
Helper Method
2010-03-11 17:05:29
A:
I don't know Java, but here is the worst case approach.
If you don't want to use split() method, then
string text = "My name is Khan";
int length = text.Length;
int numberOfWords = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == ' ')
{
numberOfWords++;
}
}
numberOfWords += 1; // no of words will be always plus one than no of space.
string[] wordCollection = new string[numberOfWords];
int wordLocation = 0;
for (int i = 0; i < text.Length; i++)
{
if (text[i] == ' ')
{
wordLocation++;
}
wordCollection[wordLocation] += text[i];
}
string reversedString = string.Empty;
foreach (string str in wordCollection)
{
reversedString = str + " " + reversedString;
}
Console.Write(reversedString.Trim());
If this is homework and its transferred to some one, then you are the looser
Mohanavel
2010-03-11 09:43:43