Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. #include <stdio.h>
  2. int ch_str(char* str, char ch);
  3. int words_counter(char* str);
  4. int longest_word(char* str);
  5. void print_words(char* str);
  6. char* reverse_words(char* str);
  7. int main(){
  8. char ch='A';
  9. //char str[10]={'A','b','2','4','f','5','g'};
  10. char str[] = "bomonka yea eee";
  11. printf("%d\n",words_count(str));
  12.  
  13.  
  14. return 0;
  15. }
  16. int ch_str(char* str, char ch)
  17. {
  18. int i = 0;
  19. while (str[i])
  20. {
  21. if (str[i] == ch)
  22. {
  23. return i;
  24. }
  25. else i++;
  26. }
  27. return -1;
  28. }
  29. int words_counter(char* str)
  30. {
  31. int i = 0, counter = 0, word = 0;
  32. char sep[] = " !?123456789,.&";
  33. while (str[i])
  34. {
  35. if ( ch_str(sep, str[i]) != -1 )
  36. {
  37. if (word)
  38. {
  39. counter++;
  40. word = 0;
  41. }
  42. }
  43. else
  44. {
  45. word = 1;
  46. }
  47. i++;
  48. }
  49. if (word) counter++;
  50. return counter;
  51. }
  52. int longest_word(char* str){
  53. int i=0;
  54. int current_len=0;
  55. int max_len=0;
  56. int counter,word = 0;
  57. char* sep = {'"','.',','};
  58. while(str[i]){
  59. if( ch_str(sep,str[i])!= -1){
  60. if (word){
  61. if(current_len>max_len)
  62. max_len=current_len;
  63. counter=0;
  64. word=0;
  65. }
  66. else {
  67. word=1;
  68. }
  69.  
  70. i++;
  71. }
  72. if(word && current_len>max_len)
  73. max_len=current_len;
  74. return max_len;
  75. }
  76. }
  77. void print_words(char* str){
  78. int i=0;
  79. int counter = 0;
  80. int word=0;
  81. char* sep = {'"','.',','};
  82. while(str[i]){
  83. if( ch_str(sep,str[i])!= -1){
  84. if (word){
  85. purchar('\n');
  86. word=0;
  87. }
  88. else {
  89. word=1;
  90. putchar(str[i]);
  91. }
  92.  
  93. i++;
  94. }
  95. }
  96. }
  97. char* reverse_words(char* str)
  98. {
  99. int i = 0, word = 0, start = 0;
  100. char sep[] = "123456789./";
  101. while (str[i])
  102. {
  103. if (ch_str(sep, str[i]) != -1)
  104. {
  105. if (word)
  106. {
  107. reverse_substr(str, start, i - 1);
  108. word = 0;
  109. }
  110. }
  111. else
  112. {
  113. if (!word)
  114. start = i;
  115. word = 1;
  116.  
  117. }
  118.  
  119. i++;
  120. }
  121. if (word)
  122. {
  123. reverse_substr(str, start, i - 1);
  124. }
  125. return str;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement