r/C_Programming • • 5h ago

Is this a VS Code newline/paste input bug?

0 Upvotes

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]", &para1[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]", &para2[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);

}


r/C_Programming • • 16h ago

Question What is C

0 Upvotes

Hey I'm a cybersecurity student just started 2 months ago

Just wanted to understand what C is as a language why is it so fast compared to other languages like py etc..

Chatgpt and yt videos didn't help me to understand :

  1. Does c talk to the hardware directly or through a compiler like other languages if yes why is it faster then ?

  2. I heard something like : c makes code that puts things in a CPU in a specific places not randomly like other languages I'm sorry I don't remember exactly ik it's confusing

  3. Do you need c for cybersecurity in the red team field

Thank you guys so much can't thank you enough for helping hope I make sense asking questions like these


r/C_Programming • • 21h ago

Question Why are assignment expressions lvalues?

4 Upvotes

I was reading up about lvalues and understand them now as something that resolves to an address(memory location) in the program. So a variable x is an lvalue (because it is mapped to some address on the stack) and similarly *x , x[i], strings are all lvalues. But if this is the case why is an expression a = b , not an lvalue? Here the expression would assign b to a and then return a as evident in assignments like so: int c = a = b. (I know that the equals sign is right associative and hence this would get resolved as c = (a = b), but that doesn't really invalidate the fact that a = b resolves to an address in the program?)