Posted By

jatkins on 09/30/11


Tagged

String count word sentence capitalization


Versions (?)

Pascal Word Count and Capitalization Sample Program


 / Published in: Pascal
 

Public domain.

  1. program word_count;
  2. uses crt;
  3. var sentence: String;
  4. i, num_words, num_five_letter_words, curr_word_length, curr_word_in_ascii, first_character: Integer;
  5. continue, conversion_type_chosen: Char;
  6. begin
  7. clrscr;
  8.  
  9. repeat
  10.  
  11. i := 0; { empty variables }
  12. num_words := 0;
  13. num_five_letter_words := 0;
  14. curr_word_length := 0;
  15.  
  16. writeln('Please enter a sentence and press return: '); { ask for sentence }
  17. readln(sentence); { store input in sentence }
  18.  
  19. for i := Length(sentence) downto 1 do { begin for loop }
  20. begin
  21. if(sentence[i]=' ') then { there's a space, so it's a new word }
  22. begin
  23. if(curr_word_length>=5) then
  24. num_five_letter_words := num_five_letter_words + 1; { increment 5 character words count }
  25.  
  26. curr_word_length := 0; { empty variable for new word }
  27. num_words := num_words + 1; { increment total word count }
  28. end
  29. else
  30. curr_word_length := curr_word_length + 1; { increment current word character count }
  31. end;
  32.  
  33. writeln;
  34. writeln('Total words: ', num_words + 1); { print total number of words }
  35. writeln('Words longer than five characters: ',
  36. num_five_letter_words); { print number of words longer than 5 characters }
  37. writeln;
  38.  
  39. write('Choose an option and press return: [U]ppercase, [L]owercase, [W]ord case');
  40. readln(conversion_type_chosen);
  41. writeln;
  42.  
  43. i := 0;
  44. first_character := 1;
  45. for i := 1 to Length(sentence) do
  46. begin
  47. curr_word_in_ascii := ord(sentence[i]);
  48. if(curr_word_in_ascii=32) then begin { character is a space, just print it }
  49. write(' ');
  50. first_character := 1;
  51. end
  52. else
  53. begin
  54. if(curr_word_in_ascii>90) then begin { character is in lower case }
  55. if(conversion_type_chosen='U')or(conversion_type_chosen='u') then
  56. write(chr(curr_word_in_ascii-32)) { convert to uppercase }
  57. else if(conversion_type_chosen='L')or(conversion_type_chosen='l') then
  58. write(chr(curr_word_in_ascii)) { convert to lowercase }
  59. else if(conversion_type_chosen='W')or(conversion_type_chosen='w') then
  60. begin
  61. if(first_character=1) then begin
  62. write(chr(curr_word_in_ascii-32));
  63. first_character := 0;
  64. end
  65. else write(chr(curr_word_in_ascii));
  66. end
  67. else
  68. write(chr(curr_word_in_ascii));
  69. end
  70. else begin { character is in upper case }
  71. if(conversion_type_chosen='L')or(conversion_type_chosen='l') then
  72. write(chr(curr_word_in_ascii+32))
  73. else if(conversion_type_chosen='W')or(conversion_type_chosen='w') then begin
  74. if(first_character=1) then begin
  75. write(chr(curr_word_in_ascii));
  76. first_character := 0;
  77. end
  78. else write(chr(curr_word_in_ascii+32))
  79. end
  80. else write(chr(curr_word_in_ascii))
  81. end;
  82. end;
  83. end;
  84. writeln;
  85. writeln;
  86. write('Enter "Y" to repeat, "N" to quit:'); { ask if they want to repeat }
  87. readln(continue); { store in continue }
  88. writeln;
  89. until(continue='N')or(continue='n'); { respond based on input }
  90.  
  91. readln;
  92. end.

Report this snippet  

You need to login to post a comment.