194,653 views
27 votes
27 votes
Write a program that inputs a line of text tokenizing the line with function strtok and outputs the tokens in reverse order i.e. last token in the sentence is printed first and first token is printed last. The user must be able to specify at most 80 characters in the sentence.

User Nawfal Serrar
by
2.6k points

1 Answer

20 votes
20 votes

Answer and Explanation:

var gettext= prompt("please enter text");

function strtok(gettext){

var splitString = gettext.split("");

if(splitString.length<=80){

var reverseArray = splitString.reverse();

var joinArray = reverseArray.join("");

return joinArray;}

else{

Alert("too many characters");

}

}

/* we first ask for user input using the prompt function. We save this in a variable gettext which we pass to the strtok function. This function first splits the text and makes it an array so we are able to count how many characters the user enters and set an if..else condition to handle characters that may be more than 80. The reverse and join functions are then used respectively to reverse and return the string.*/

User MarkReedZ
by
3.4k points