r/C_Programming • u/Real-Nebula-2074 • Oct 15 '26
Is this a VS Code newline/paste input bug?
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);
}
3
u/mikeblas Oct 15 '26
Please correctly format your code. It's Rule #1 of this sub.
I'm not sure what you expect of this code:
or this:
scanf()doesn't support regular expressions, if that's what you're trying.Another troubling spot is this declaration
which takes an array of 21 pointers. That probably menat to be an array of pointers to 21. The code just looks at the first character of each string. What is it meant to do?
But when you call it like this:
you're passing the length of one string, and then passing the first pointer within an array of other pointers that ... well, it's such a mess that I'm not really sure what it is you're trying to do.
But, to answer your question: you have provided no evidence of any bug in Visual Studio Code.
6
u/tastygames_official Oct 15 '26
I didn't read the text but I just had to format your code properly, sorry...
2
u/mjmvideos Oct 15 '26
Run it in the debugger and step through it. (Best) or add some print statements to see what’s happening (second best)
1
u/Own_Unit_6170 Oct 16 '26 edited Oct 16 '26
Here's a C version (instead of my previous C++ version). Note that scanf() is replaced by fgets(). A parsing routine extracts (and lowercases) the words from each paragraph, instead of relying on scanf(). Sorting the word lists allows common words to be found in one pass and additionally provides sorted output.
1
u/zomerbk Oct 17 '26
The second Enter doesn’t finish the first paragraph because the scanset `scanf("%[\n]", shit)` only succeeds if the *next* character is a newline. Your preceding scanset, `scanf("%*[^\nA-Za-z]")`, stops before that newline, so the first Enter prints `stage1` but leaves the newline unread. On the next loop, `%[A-Za-z]` fails immediately on that leftover newline; since you don’t check its return value, the later scans then behave unexpectedly. Check every `scanf` result, and for line-based input use `fgets` to read each paragraph, then tokenize it. So it’s the input logic, not a VS Code newline bug.
0
u/kutac56 Oct 15 '26
I didn't read much of what you yapped, but you used scanf which leaves behind a new line character when you use it again which is prob the issue. Use fgets instead. Scanf is unsafe for this reason
0
u/21Ali-ANinja69 Oct 16 '26
Two scanfs will get you your input. Never assume how a fuction works. Scanf for a string will go until it hits a newline
-2
u/[deleted] Oct 15 '26
[removed] — view removed comment