Guest User

Untitled

a guest
Dec 9th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. int main(void)
  2. {
  3. int c, countSpaces = 0;
  4.  
  5. printf("Type sentence:n");
  6.  
  7. do
  8. {
  9. c = getchar();
  10. if (c == ' ')
  11. countSpaces = countSpaces + 1;
  12. }
  13. while (c != 'n');
  14.  
  15. printf("Sentence contains %d Spaces.n", countSpaces);
  16.  
  17. return 0;
  18. }
  19.  
  20. if (c != EOF)
  21. countSpaces = countSpaces + 1;
  22. }
  23. while (c != 'n');
  24.  
  25. printf("Sentence contains %d Spaces.n", countSpaces - 1);
  26.  
  27. #include <stdio.h>
  28.  
  29. int main(void)
  30. {
  31. int c;
  32. int countSpaces = 0;
  33. int countChars = 0;
  34.  
  35. puts("Type sentence:");
  36.  
  37. do {
  38. c = getchar();
  39. countChars += 1;
  40. if (c == ' ') {
  41. countSpaces += 1;
  42. }
  43. } while (c != 'n');
  44.  
  45. printf("Sentence contains %d spaces and %d characters.n", countSpaces, countChars);
  46.  
  47. return 0;
  48. }
  49.  
  50. if( condition )
  51. do something
  52. whoops this is not in the condition but it sure looks like it is!
  53.  
  54. $ ./test
  55. Type sentence:
  56. foo bar baz
  57. Sentence contains 2 spaces and 12 characters.
  58.  
  59. // Note, the parenthesis around `c = getchar()` are important.
  60. while( (c = getchar()) != 'n' ) {
  61. countChars++;
  62. if (c == ' ') {
  63. countSpaces++;
  64. }
  65. }
  66.  
  67. $ ./test
  68. Type sentence:
  69. foo bar baz
  70. Sentence contains 2 spaces and 11 characters.
  71.  
  72. #include <stdio.h>
  73.  
  74. int main()
  75. {
  76. char *str = calloc(sizeof(char),50);
  77. int i = 0, count = 0;
  78.  
  79. printf("Type sentence:n");
  80. scanf("%s",str);
  81.  
  82. while (str[i++] != '')
  83. count++; //length of the string
  84.  
  85. printf("%d",count);
  86. return 0;
  87. }
  88.  
  89. #include <stdio.h>
  90.  
  91. int main(void)
  92. {
  93. int i;
  94. int length = 0;
  95. char buffer[1024];
  96.  
  97. printf( "Enter some text> " );
  98. fgets( buffer, sizeof(buffer), stdin );
  99.  
  100. // If the user inputs > 1024 letters, buffer will not be n terminated
  101. for ( i=0; buffer[i] != 'n' && buffer[i] != ''; i++ )
  102. {
  103. length += 1;
  104. }
  105.  
  106. printf( "length: %dn", length );
  107.  
  108. return 0;
  109. }
Add Comment
Please, Sign In to add comment