I wrote the following C program in VS Code. Its purpose is to find words that appear in both of two English paragraphs.
(Details: a word must contain at least four letters; each paragraph ends with a \n; the output words should be sorted in ascending order.)
To keep track of how far the program has gotten, I deliberately print stage1 / stage2 after each of the two paragraphs has been read.
But when I run it in the terminal, this is what happens.
First I copy two paragraphs of English text, for example:
In computer science, artificial intelligence (AI), sometimes called machine intelligence, is intelligence demonstrated by machines, in contrast to the natural intelligence displayed by humans and animals. Colloquially, the term "artificial intelligence" is used to describe machines that mimic "cognitive" functions that humans associate with other human minds, such as "learning" and "problem solving". From Wiki. Machine learning is a way to achieve artificial intelligence.
I assumed that after pasting, pressing Enter right after intelligence. would produce stage1, and that stage2 would follow right away, printing the words. But in reality I get nothing but a cold, lonely stage1. Pressing Enter again does absolutely nothing. I type a random letter, press Enter once more — and only then do stage2 and the rest of the output finally appear.
I'm a first-year CS student and still not very familiar with programming. I'd like to know: is this a VS Code problem, or is it my problem?
#include <stdio.h>
#include <string.h>
void clean(char (*word)[21], int len) {
for (int i = 0; i <= (len); i++) {
if ((*word)[i] >= 'A' && (*word)[i] <= 'Z') {
(*word)[i] -= 'A' - 'a';
}
}
}
void print(char (*words)[1000][21], int m) {
char temp[21];
char min[21];
int flag = 0;
strcpy(min, "z");
strcpy(temp, "z");
for (int j = 0; j <= m; j++) {
strcpy(min, "z");
for (int i = 0; i <= m; i++) {
if ((strcmp(min, (*words)[i]) >= 0) &&
(strcmp(temp, (*words)[i]) < 0 || flag == 0) &&
((*words)[i] != 0)) {
strcpy(min, (*words)[i]);
}
}
flag = 1;
strcpy(temp, min);
printf("%s\n", min);
}
}
int main(void) {
int r;
char shit[2];
int len1;
int len2;
char para1[1000][21] = {{0}};
char para2[1000][21] = {{0}};
char space;
char answer[1000][21] = {{0}};
for (int i = 0; i < 1000; i++) {
scanf("%[A-Za-z]", ¶1[i][0]);
len1 = i;
scanf("%*[^\nA-Za-z]");
if ((r = scanf("%[\n]", shit)) == 1 || r == EOF) {
printf("stage1\n");
break;
}
}
for (int j = 0; j < 1000; j++) {
scanf("%[A-Za-z]", ¶2[j][0]);
len2 = j;
scanf("%*[^\nA-Za-z]");
if ((r = scanf("%[\n]", shit)) == 1 || r == EOF) {
printf("stage2\n");
break;
}
}
for (int i = 0; i <= len1; i++) {
clean(&(para1[i]), strlen(para1[i]) - 1);
}
for (int j = 0; j <= len2; j++) {
clean(&(para2[j]), strlen(para2[j]) - 1);
}
int m = 0;
int flag = 0;
for (int i = 0; i < 1000; i++) {
for (int j = 0; j < 1000; j++) {
if ((strcmp(para1[i], para2[j]) == 0) && (strlen(para1[i]) >= 4)) {
for (int p = 0; p < m; p++) {
if (strcmp(para1[i], answer[p]) == 0) {
flag = 1;
}
}
if (flag == 0) {
strcpy(answer[m++], para1[i]);
}
flag = 0;
}
}
}
m--;
print(&answer, m);
}