Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5. char* str = new char[256];
  6. cin.getline(str, 256);
  7. char** wordsArray = new char*[15];
  8. for (int i = 0;i < 15;i++) wordsArray[i] = new char[15]; //15 random value
  9. int wordsCount = 1;
  10. for (int i = 0;i < strlen(str);i++)
  11. {
  12. if (str[i] == ' ')
  13. {
  14. wordsCount++;
  15. }
  16. }
  17. int* wordsLengthTab = new int[wordsCount] {0}; //creating table of every word length
  18. int posInTab = 0, val = 0;
  19. for (int i = 0;i < strlen(str);i++)
  20. {
  21. if (str[i] != ' '){
  22. val++;
  23. wordsLengthTab[posInTab] = val;
  24. }
  25. else{
  26. posInTab++;
  27. val = 0;
  28. }
  29. }
  30. int min = wordsLengthTab[0];
  31. int pos=0;
  32. for (int i = 0;i < wordsCount;i++)
  33. {
  34. if (wordsLengthTab[i] < min)
  35. {
  36. min = wordsLengthTab[i];
  37. pos = i + 1; //needed word position in textline
  38. }
  39. }
  40. cout << "Number of word in string is: " << pos << endl;
  41. char* finalWord = new char[min] {'\0'}; //for minimal word output
  42. int finalCount = 1;
  43. for (int i = 0;i < strlen(str);i++)
  44. {
  45. if (str[i] == ' ')
  46. {
  47. finalCount++;
  48. if (finalCount == pos)
  49. {
  50. i++;
  51. int chr = 0;
  52. while (chr < min)
  53. {
  54. finalWord[chr] = str[i];
  55. i++;
  56. chr++;
  57. }
  58. break;
  59. }
  60. }
  61. }
  62. cout << "Your word is: ";
  63. for (int i = 0;i < min;i++)
  64. cout << finalWord[i];
  65. cout << endl;
  66. for (int i = 0;i < 15;i++)delete[]wordsArray[i];
  67. delete[]wordsArray;
  68. delete[]wordsLengthTab;
  69. delete[]finalWord;
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement