Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 14 / task7.cpp
blob: fce9e8b26c408cfcbdbd0121555a1008ff8fb001 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
using namespace std;

const int MAXLEN =  1024;


int fillWords(const char* str, char words[MAXLEN][MAXLEN])
{
    int len = strlen(str);
    
    int currentWordCursorInMatrix = 0;
    int count = 0 ; 
    
    for(int i = 0; i < len; i++)
    {
        if(str[i] == ' ')
        {
            if(currentWordCursorInMatrix != 0)
            {
                words[count][currentWordCursorInMatrix] = '\0';
                count++;
                currentWordCursorInMatrix = 0;
            }
        }
        else
        {
            words[count][currentWordCursorInMatrix] = str[i];
            currentWordCursorInMatrix++;
        }
    }
    
    return count;
    
}

void createReversedSentence(char words[MAXLEN][MAXLEN], int wordsCount,  char* newSentence)
{
    
    for (int j = wordsCount - 1; j >= 0 ; j--)
    {
	    strcat(newSentence, words[j]);
	    strcat(newSentence, " ");
    }
}
int main()
{
    const char* str = "How are you   friends. I am fine!";
    
    char words[MAXLEN][MAXLEN];
    
    int wordsCount = fillWords(str, words);

    char* newSentense = new char[strlen(str)];
    
    createReversedSentence(words, wordsCount, newSentense);
    
    cout << newSentense << endl;
    
    delete[] newSentense;
    
    return 0;
}