count blanks tabs newlines


/ Published in: C
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #include <stdio.h>#include <string.h>
  2.  
  3. /* Version mit vorgegebenen Text
  4. int main (int argc, const int *argv){
  5.   int i=0, tab=0, blank=0, nl=1;
  6.   char text[160];
  7.  
  8.   printf("\nBitte geben Sie einen Text ein\n");
  9.   strcpy(text,"was machen wir heute noch?\nich\tweis\tnicht.");
  10.   while(text[i]){
  11.   if(text[i]=='\t')
  12.   tab++;
  13.   else if(text[i]==' ')
  14.   blank++;
  15.   else if(text[i]=='\n')
  16.   nl++;
  17.   i++;
  18.   }
  19.   printf("\n'%s'\nhat %i Leerzeichen, %i Tabulatorstellen und %i Zeilen", text, blank, tab, nl);
  20. }
  21. */
  22.  
  23.  
  24. int main (int argc, constchar * argv[]){
  25. int c, tab=0, nl=1, blank=0;
  26.  
  27.  
  28. while((c=getchar())!=EOF){
  29. if(c==' ')
  30. blank++;
  31. else if(c=='\t')
  32. tab++;
  33. else if(c=='\n')
  34. nl++;
  35. }
  36. printf("\nDer Text hat %i Leerzeichen, %i Tabulatorstellen und %i Zeilen", blank, tab, nl);
  37. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.