#strings in c

1 messages · Page 1 of 1 (latest)

analog sun
#

how can i write a program that keep asking the user to enter strings and stop if the user entered two strings equal one after one

analog sun
#
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int main()
{
char N1[25],N2[25];
bool B;
printf("Enter a name: ");
scanf("%s",N1);
B=false;
while (B==false&&N1[0]!='F'&&N1[1]!='i'&&N1[2]!='n'){
    strcpy(N2, N1);
    printf("Enter a name: ");
    scanf("%s",N1);
    if (strcmp(N1, N2)==0){
        B=true;
        printf("Found It");
    }
}
printf("Done");
}
#

i want if the user entered the string'Fin' the loop will stop

#

i have tried so hard

sinful quest
#

You kinda not too far from a solution

analog sun
#

and i couldn't figure it out

#

i know

sinful quest
#

But it's way to complex for what you're trying to do.

#

Give me a min I'll do something and explain

analog sun
#

so..

#

ok take ur time

sinful quest
#

Okay, so first of all, you only need 2 variables to make this work. Your first and second string. Both will have default values.

What you want to do, is for your while loop to continue until your 2 string are equal to one another. So your condition is simple, you simply have to check if the 2 strings are not equals.

In each iteration, you will prompt your user to enter the first string and your second string. You will resign your 2 string with those values to the variables used in your while loop.

Then, once the 2 string are equal, the loop will simply stop.

analog sun
#

i want that if the user entered the string'Fin' the loop will stop

#

how can i do it?

sinful quest
#

You want to stop only if the user enters "Fin"?

#

It's similar. Only the condition in your loop changes.

#

Yea compare the value entered by the user to "Fin". Check this pseudocode, maybe it will help you understand.

string user_input = ""

while (user_input not equal "Fin") {
  ask user_input
}
#

It shouldn't be more complicated than that in C. (hint you want to use strcmp in C)

#

Do you understand?

analog jackal
#

ALWAYS initialize your variable, ALWAYS

#

never use, strcpy() or strcat() or scanf() WITHOUT buffer size

#

&&N1[0]!='F'&&N1[1]!='i'&&N1[2]!='n')

this will match "Fin" and "Final" and "Find"

#

you forgot to check for \0

#

so might as well use strncmp() here

analog sun
analog jackal
#

also

analog sun
analog jackal
#

char N1[25]={0},N2[25]={0};
bool B=false;
#

ALWAYS init variables

#

no EXCEPTION

sinful quest
# analog sun not really
#include <stdio.h>
#include <string.h>

int main() {
    char name[25] = {0};
    
    // We loop as long as name is not equal to "Fin"
    while(strcmp(name, "Fin") != 0) {
        // We ask the name to the user
        printf("Entrer a name (\"Fin\" to quite): ");
        scanf("%24s", name);
    }
    
    printf("Done!");
}
analog jackal
#

also your scanf should specify the size

#

so "%24s"

#

otherwise it will read 4096 bytes into 25 byte buffer and overflow

analog sun
analog jackal
#

string up to 24 bytes + \0 = 25 bytes total

analog sun
#

ah i get it

analog jackal
#

it's = {0}

#

not = ""

analog sun
#

?

analog jackal
#

as in fill the array with '\0' (NUL byte)

sinful quest
#

I can change it if you want, haven't wrote anything in C in a long time XD

analog jackal
#

also add \n

#

otherwise it won't be on new lines

#

also you might want to fflush(STDOUT);

#

what is the goal of your while loop ?

sinful quest
analog jackal
#

maybe the scanf() \n was outputted

sinful quest
#

I think it does

analog jackal
#

because you typed "Malassi\n"

sinful quest
#

No I don't

analog sun
#

but wy should i always ALWAYS inti my variables?? can u explain?

analog jackal
#

SECURITY BUG

sinful quest
analog jackal
#

and UNDEFINED BEHAVIOR

#

and VARIABLE filled up with GARBAGE

#

in C/C++, if you ask the compiler to do something stupid, it will do just that

#

and trust that stupidity is allowed, no warning, no error

#

just pure madness

#

If compiled in DEBUG mode

#

it might fill up your variable with 0xCDCD CDCD

#

or 0xCCCC CCCC

#

or 0xDDDD DDDD

#

to tell you that you are stupid

#

but in PRODUCTION (non-Debug) mode it will be pure garbage

#

your teacher is teaching you ANSI C from 1980

#

nobody should teach C that way anymore

#

or at least show you "before C" to "new C"

#

best is strlcpy() / strlcat() from OpenBSD

#

otherwise strncpy() / strncat() with 0 padding arithmetic

#

or _s() alternative from MSVC++

#

which is like C11 ~ 2011

#

also only constants should be in CAPSLOCK 🙂

#

;compile c

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#define MAX_LENGTH 25
#define MAX_SCANF  "%24s"
int main()
{
    char name1[MAX_LENGTH] = {0};
    char name2[MAX_LENGTH] = {0};
    bool b = false;
    
    printf("\nEnter a name: ");
    scanf(MAX_SCANF, name1);

    while(!b && strncmp("Fin", name1, MAX_LENGTH) != 0)
    {
        // Copy last input into current ?!
        strncpy(name2, name1, MAX_LENGTH);

        printf("\nEnter a name: ");
        scanf(MAX_SCANF, name1);
        if (strncmp(name1, name2, MAX_LENGTH) == 0)
        {
            printf("\nFound It\n");
            b = true;
        }
    }

    printf("\nDone\n");
    return 0;
}
hazy brookBOT
#
Program Output
Enter a name: 
Enter a name: 
Found It

Done
analog jackal
#
#
Enter a name: abc

Enter a name: def

Enter a name: def

Found It

Done
#

Is that the supposed behavior?

analog sun
analog sun
analog jackal
#

basically the standard is C11 so 2011

#

and it is known since the late 90s and even before via OpenBSD

#

Does my code does what you want or what are your requirements?

analog sun
#

oh

analog jackal
analog sun
#

well i have checked it

#

and yes it is working

#

but i already made my one

#

i want you to check it

#

and tell me which is the best

#
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
int main()
{
char N1[25],N2[25];
bool B;
printf("Enter a name: ");
scanf("%24s",N1);
B=false;
while (B==false&&strcmp(N1,"Fin")!=0){
    strcpy(N2, N1);
    printf("Enter a name: ");
    scanf("%24s",N1);
    if (strcmp(N1, N2)==0){
        B=true;
        printf("Found It");
    }
}
printf("\nDone");
}
analog jackal
#

you DID not initialize your variables

#

use strncmp(,, 25)

#

also return 0;

analog sun
analog sun
analog jackal
#

Variable in C/C++ have NO DEFAULT value

#

Variable in PHP, JavaScript, Java, C#, Perl have a default value

#

C/C++ is not a managed programming language

#

and the list goes on and on.

#

CWE-457: Use of Uninitialized Variable
The code uses a variable that has not been initialized, leading to unpredictable or unintended results.

In some languages such as C and C++, stack variables are **not initialized by default. **

They generally **contain junk data **with the contents of stack memory before the function was invoked.

An attacker can sometimes control or read these contents.

In other languages or conditions, a variable that is not explicitly initialized can be given a default value that has security implications,
depending on the logic of the program.

The presence of an uninitialized variable can sometimes indicate a typographic error in the code.

analog sun
analog sun
#

well i think i got it now

#

thank you very much for ur explanation