Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main()
- {
- char* str = new char[256];
- cin.getline(str, 256);
- char** wordsArray = new char*[15];
- for (int i = 0;i < 15;i++) wordsArray[i] = new char[15]; //15 random value
- int wordsCount = 1;
- for (int i = 0;i < strlen(str);i++)
- {
- if (str[i] == ' ')
- {
- wordsCount++;
- }
- }
- int* wordsLengthTab = new int[wordsCount] {0}; //creating table of every word length
- int posInTab = 0, val = 0;
- for (int i = 0;i < strlen(str);i++)
- {
- if (str[i] != ' '){
- val++;
- wordsLengthTab[posInTab] = val;
- }
- else{
- posInTab++;
- val = 0;
- }
- }
- int min = wordsLengthTab[0];
- int pos=0;
- for (int i = 0;i < wordsCount;i++)
- {
- if (wordsLengthTab[i] < min)
- {
- min = wordsLengthTab[i];
- pos = i + 1; //needed word position in textline
- }
- }
- cout << "Number of word in string is: " << pos << endl;
- char* finalWord = new char[min] {'\0'}; //for minimal word output
- int finalCount = 1;
- for (int i = 0;i < strlen(str);i++)
- {
- if (str[i] == ' ')
- {
- finalCount++;
- if (finalCount == pos)
- {
- i++;
- int chr = 0;
- while (chr < min)
- {
- finalWord[chr] = str[i];
- i++;
- chr++;
- }
- break;
- }
- }
- }
- cout << "Your word is: ";
- for (int i = 0;i < min;i++)
- cout << finalWord[i];
- cout << endl;
- for (int i = 0;i < 15;i++)delete[]wordsArray[i];
- delete[]wordsArray;
- delete[]wordsLengthTab;
- delete[]finalWord;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement