Split lines into words task - linear search method

71 views Asked by At

I've been given a task to split lines into words and then split the lines up based on spaces and newlines. I've came up with an incomplete solution as it will not print the last word. I can only use linear search hence the basic approach.

line = raw_input()
while line != "end":
   i = 0
   while i < len(line):
      i = 0
      while i < len(line) and line[i] == " ":
         i = i + 1
      j = i 
      while line[j] != " ":
        j = j + 1
      print line[i:j]
      line = line[j:]
   line = raw_input()
1

There are 1 answers

0
Akshay Kathpal On

I understand your problem this is somewhat similar to the Hackerearth problem


See this example to clarify your concept


y=list()
1). y=map(int,raw_input().split()) # for storing integer in list 
2). y=map(str,raw_input().split()) # for storing characters of string in list
#then use this to print the value 
for i in y:
    print i #this loop will print one by one value
#working example of this code is #for Second part
>>baby is cute #input
>>['baby','is','cute'] #output
>> #now according to your problem line is breaks into words

If you find it useful Thumbs up