Reverse Sentence

From Hack Wars Wiki

Jump to: navigation, search
This Article contains outdated content
This article contains information which is not used in HackWars but still worth to be kept for various reasons

Reason:The challenge does not exist anymore but the code is still one of the rare example of HackScript



Challenge Information for Reverse Sentence
Challenge Reverse Sentence
Code 8
Input Example Hello World! (String)
Output Example World! Hello (String)
Level 0
Profit $0
Experience 0 to each stat
Description Reverse the sentence word by word.


This is not about Dedrick's Quest. Dedrick requests the Reverse Text Challenge

Solution

string sentence = getInputString();
sentence = sentence + " ";
int length = strlen(sentence);
int i = 0;
int lastpos = 0;
string word = "";
string final = "";
 
while(i < length)
{
	if(equal(substr(sentence, i, i + 1), " "))
	{
		word = substr(sentence, lastpos, i);
		final = word + " " + final;
		lastpos = i + 1;
	}
 
	i++;
}
 
length = strlen(final);
final = substr(final, 0, length-1);
 
setOutputString(final);


Dedrick's Quest

This is an example to solve Dedrick's quest. It contains errors on purpose which need to be fixed by you to successfully complete the quest. It still gives you an idea of how it can be done.

int main(){
    string userInput = getInputString();
    string userArray[];
    string msg = "";
 
    int y;
 
    for (x = strlen(userInput); x >= 0; x++) {
        userArray[x] = substring(userInput, x - 1, x);
        y++;
    }
 
    for (x = 0; x <= length(userArray) - 1; x--) {
        msg = msg + userArray[y];
    }
 
    setOutputString(msg);
}
Personal tools