0
Does anyone know how to design a program that reads a string and a ineteger number k and tell us how many words have a lenght of
the program doesn't have to use split() method only len() method
4 odpowiedzi
+ 3
Program is easy. You need a count of words the right length initialized to zero. You need a count of letters in the current word initialized to zero. You need to declare and read your two inputs.
For each character in the string
If the character is a space
If the letter count matches k
Increment the word count
Endif
Zero the letter count
Else
Increment the letter count
Endif
Endfor
If the letter count matches k
Increment the word count
Endif
Print the word count
+ 2
Didn't get your question. Do you want to get the length of entire string? If yes then you can use len() method in python or similar in other languages.
0
I need to split the string in words and to determine how many words have the lenght of k, but I don't want to use the split() method
print u'Programa para la busqueda de palabras de cierta longitud'
print u'Escribe fin, para salir.'
cadena = ' '
k = 0
while cadena != '' and k != '':
cadena = raw_input('\nEscribe una frase: ')
try:
k = int(raw_input('\nDime el n\243mero de caracteres a buscar: '))
except ValueError:
print u'Dato erroneo.'
continue
if cadena == 'fin':
break
contar = 0
cadena = cadena.strip().split()
for palabra in cadena:
if len(palabra) == k:
contar = contar + 1
print u'\nHay {0} palabra(s) con longitud de {1} caracteres.'.format(contar,k)
print u'\nGracias por usar el programa, Adios.'
0
with Python I'm a beginner