Question: Java Removing leading / trailing whitespace Users may type or copy - paste in a text box of a web form, perhaps a name like
Java Removing leadingtrailing whitespace Users may type or copypaste in a text box of a web form, perhaps a name like "Joe Smith". The user sometimes has whitespace before or after the text, like Joe Smith A program typically strips such leading and trailing whitespace. Given a string, create a new string that lacks any leading or trailing spaces. Given Joe Smith the new string should be "Joe Smith". Be sure to handle the case of the user entering only whitespace, or entering nothing, which each should yield an empty string Hints: Initially, try to find the index of the first nonspace character. Set the index to then use a while loop to check each character. Your while loop expression should first check that the index is valid for the size of the string. The size function of a string returns an unsigned int, so declare your index variable as an unsigned int so you can compare without a compiler warning. Next, try to find the index of the last nonspace character. Use a different index variable. You while loop expression should first check that it is greaterthanorequal to to ensure it is valid. Once you know the first and last nonspace characters, you can use a for loop to copy those characters to the new string. After the first while loop, be sure to check if any nonspace character was found by checking if the index is less than the string's last element If no nonspace character was found, just return.
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
