I need to cout random word which starts with 'k; I have decided to count the amount of words and amount of words(beginning with 'k'); After that i made it into an array, so i could have an array full of the number of words which start with 'k'; But here is the problem, compiler doesn't let the code to go on and i can't understand what's the problem; I'm a beginner, so go easy on me; Here is the code:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdlib.h>
#include <ctime>
#include <cmath>
#include <cstring>
#include <clocale>
#include <Windows.h>
using namespace std;
int main()
{
srand(time(NULL));
char str1[150] = "knight kevin once said that maybe that kind of process is necessary to him pushing him on and on and it is very painful he knows.";
cout << str1 << endl;
int sz = 1;
int s = 1;
int sz_count = 1;
int s_count = 0;
int x = 1;
int* amounts_of_words = new int[s];
int* array_of_K = new int[sz];
if (str1[0] == 'k' || str1[0] == 'K') {
array_of_K[0] = 1;
sz++;
}
for (int i = 0; str1[i] != '\0'; i++) {
if (str1[i] == ' ' || str1[i] == '.' || str1[i] == '?' || str1[i] == '!') {
amounts_of_words[s_count] = x;
s++;
x++;
if (str1[i + 1] == 'k') {
array_of_K[sz_count] = amounts_of_words[s_count] + 1;
sz++;
sz_count++;
}
s_count++;
}
if (str1[i + 1] == '\0') {
sz--;
s--;
}
}
for (int f = 0; f < sz; f++) {
cout << array_of_K[f] << " ";
}
char* token;
int randomN = rand() % 4;
cout << randomN;
cout << array_of_K[randomN];
for (int i = 1; i <= s; i++) {
if (i == 1 && i == array_of_K[randomN]) {
token = strtok(str1, " ");
cout << token;
break;
}
else if (i == 0) {
token = strtok(str1, " ");
}
else {
token = strtok(NULL, " ");
if (i == array_of_K[randomN]) {
cout << token;
break;
}
}
}
delete[] array_of_K;
delete[] amounts_of_words;
}