/ Published in: Pascal
Public domain.
Expand |
Embed | Plain Text
program word_count; uses crt; var sentence: String; i, num_words, num_five_letter_words, curr_word_length, curr_word_in_ascii, first_character: Integer; continue, conversion_type_chosen: Char; begin clrscr; repeat i := 0; { empty variables } num_words := 0; num_five_letter_words := 0; curr_word_length := 0; writeln('Please enter a sentence and press return: '); { ask for sentence } readln(sentence); { store input in sentence } for i := Length(sentence) downto 1 do { begin for loop } begin if(sentence[i]=' ') then { there's a space, so it's a new word } begin if(curr_word_length>=5) then num_five_letter_words := num_five_letter_words + 1; { increment 5 character words count } curr_word_length := 0; { empty variable for new word } num_words := num_words + 1; { increment total word count } end else curr_word_length := curr_word_length + 1; { increment current word character count } end; writeln; writeln('Total words: ', num_words + 1); { print total number of words } writeln('Words longer than five characters: ', num_five_letter_words); { print number of words longer than 5 characters } writeln; write('Choose an option and press return: [U]ppercase, [L]owercase, [W]ord case'); readln(conversion_type_chosen); writeln; i := 0; first_character := 1; for i := 1 to Length(sentence) do begin curr_word_in_ascii := ord(sentence[i]); if(curr_word_in_ascii=32) then begin { character is a space, just print it } write(' '); first_character := 1; end else begin if(curr_word_in_ascii>90) then begin { character is in lower case } if(conversion_type_chosen='U')or(conversion_type_chosen='u') then write(chr(curr_word_in_ascii-32)) { convert to uppercase } else if(conversion_type_chosen='L')or(conversion_type_chosen='l') then write(chr(curr_word_in_ascii)) { convert to lowercase } else if(conversion_type_chosen='W')or(conversion_type_chosen='w') then begin if(first_character=1) then begin write(chr(curr_word_in_ascii-32)); first_character := 0; end else write(chr(curr_word_in_ascii)); end else write(chr(curr_word_in_ascii)); end else begin { character is in upper case } if(conversion_type_chosen='L')or(conversion_type_chosen='l') then write(chr(curr_word_in_ascii+32)) else if(conversion_type_chosen='W')or(conversion_type_chosen='w') then begin if(first_character=1) then begin write(chr(curr_word_in_ascii)); first_character := 0; end else write(chr(curr_word_in_ascii+32)) end else write(chr(curr_word_in_ascii)) end; end; end; writeln; writeln; write('Enter "Y" to repeat, "N" to quit:'); { ask if they want to repeat } readln(continue); { store in continue } writeln; until(continue='N')or(continue='n'); { respond based on input } readln; end.
You need to login to post a comment.
