User:Fako Berkers/Firstletter: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 1: Line 1:
<code source="python">
[[Category:prototyping]]
<source lang="python">
text = "He saw the cat before he saw the potato."
text = "He saw the cat before he saw the potato."
rsl = ""
rsl = ""
whtsp = True # saves the first character
whtsp = True # starting with True saves the first character


for char in text:
for char in text:
Line 17: Line 18:
print text
print text
print rsl
print rsl
</code>
</source>

Latest revision as of 18:21, 26 April 2011

text = "He saw the cat before he saw the potato."
rsl = ""
whtsp = True # starting with True saves the first character

for char in text:
	
	if whtsp: # true if previous was a whitespace (or first char)
		whtsp = False
		rsl += char
	else: # previous char wasn't whitespace, save whitespace.
		rsl += ' '
	
	if char == ' ': # check if current was whitespace.
		whtsp = True

print text
print rsl