i)Write one line statement using an appropriate function which checks whether the character ch is a White space or not? ii)Write statement to find and display the position of the
space occurring for second last time
Answers
Answer:
Method 1: HashMap and Two-string method traversals.
Approach: A character is said to be non-repeating if its frequency in the string is unit. Now for finding such characters, one needs to find the frequency of all characters in the string and check which character has unit frequency. This task could be done efficiently using a hash_map which will map the character to there respective frequencies and in which we can simultaneously update the frequency of any character we come across in constant time. The maximum distinct characters in the ASCII system are 256. So hash_map has a maximum size of 256. Now read the string again and the first character which we find has a frequency as unity is the answer
Explanation:
Step 1: Construct a character count array
from the input string.
....
count['e'] = 4
count['f'] = 1
count['g'] = 2
count['k'] = 2
……
Step 2: Get the first character who's
count is 1 ('f').