Advertisement
Guest User

es 7

a guest
Oct 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. char ch,ch_pre;
  6. int righe=1,parole=0,caratteri=0;
  7. ch_pre='\n';
  8.  
  9. printf("Inserisci testo (per terminare EOF)\n");
  10.  
  11. while((ch=getchar())!=EOF){
  12.  
  13. switch(ch){
  14.  
  15. case '\n':
  16.  
  17. if((ch_pre=='\n')||(ch_pre=='\t')||(ch_pre==' ')){
  18. righe++;
  19. ch_pre=ch;
  20. break;
  21. }
  22. righe++;
  23. parole++;
  24. ch_pre=ch;
  25. break;
  26.  
  27.  
  28. case '\t':
  29.  
  30. if((ch_pre=='\n')||(ch_pre=='\t')||(ch_pre==' ')){
  31.  
  32. ch_pre=ch;
  33. break;
  34. }
  35. parole++;
  36. ch_pre=ch;
  37. break;
  38.  
  39.  
  40.  
  41. case ' ':
  42.  
  43. if((ch_pre=='\n')||(ch_pre=='\t')||(ch_pre==' ')){
  44.  
  45. ch_pre=ch;
  46. }
  47. parole++;
  48. ch_pre=ch;
  49. break;
  50.  
  51.  
  52. default:
  53. caratteri++;
  54. ch_pre=ch;
  55. break;
  56. }
  57. }
  58. /* per come ho scritto il codice se inserisco EOF senza inserire come ultimo carattere ' ' o ' \n' o '\t' non viene
  59. contata l'ultima parola e perciò:*/
  60. if((ch_pre!='\n')||(ch_pre!='\t')||(ch_pre!=' ')){
  61. parole++;
  62. }
  63.  
  64. printf("\n");
  65. printf("Numero righe :%d ; Numero parole :%d ; Numero caratteri :%d \n",righe,parole,caratteri);
  66. return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement