Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. const int MAX_SIZE = 100;
  4. int sizeOfArr(const char* arr) {
  5. int size = 0;
  6. for (int i = 0; arr[i] != '\0'; i++) {
  7. size++;
  8. }
  9. return size;
  10. }
  11.  
  12. void shortestWords(const char* string, int sizeOfStr) {
  13.  
  14. int shortestWord = sizeOfStr;
  15. int numOfShortest = 1;
  16. int currentSize = 0;
  17. for (int i = 0; i<=sizeOfStr; i++) {
  18.  
  19. if (string[i] != ' ' && string[i] != '\t' && string[i]!='\0' )
  20. {
  21. if ((string[i] > 'a' && string[i] < 'z') || (string[i] > 'A' && string[i] < 'Z') || string[i] == '-' || string[i] == '_') {
  22. currentSize++;
  23. }
  24. else {
  25. while (string[i] != ' ' && string[i] != '\t' && string[i] != '\0') {
  26. i++;
  27. currentSize = 0;
  28. }
  29. }
  30.  
  31. }
  32. else {
  33. if (currentSize < shortestWord) {
  34. shortestWord = currentSize;
  35. numOfShortest = 1;
  36. }
  37. else {
  38. if (currentSize == shortestWord)
  39. {
  40. numOfShortest++;
  41. }
  42. }
  43. currentSize = 0;
  44. }
  45.  
  46. }
  47. cout << "Shortest word is: " << shortestWord << endl;
  48. cout << "Number of shortest words is:" << numOfShortest << endl;
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54. char arr[MAX_SIZE];
  55. cout << "Input string: ";
  56. cin.getline(arr, MAX_SIZE);
  57. int length = sizeOfArr(arr);
  58.  
  59. shortestWords(arr, length);
  60. system("pause");
  61. return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement