#strings in c
1 messages · Page 1 of 1 (latest)
Have you tried anything?
#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
You kinda not too far from a solution
But it's way to complex for what you're trying to do.
Give me a min I'll do something and explain
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.
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?
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
exactlty that is the problem that i found
also
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!");
}
also your scanf should specify the size
so "%24s"
otherwise it will read 4096 bytes into 25 byte buffer and overflow
%24s??
string up to 24 bytes + \0 = 25 bytes total
ah i get it
?
as in fill the array with '\0' (NUL byte)
I can change it if you want, haven't wrote anything in C in a long time XD
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 ?
There should be... at least my compiler is giving me a new line without the \n
maybe the scanf() \n was outputted
I think it does
because you typed "Malassi\n"
No I don't
but wy should i always ALWAYS inti my variables?? can u explain?
SECURITY BUG
Anyway, it's not important
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;
}
Enter a name:
Enter a name:
Found It
Done
Online GDB is online ide with compiler and debugger for C/C++. Code, Compiler, Run, Debug Share code nippets.
Enter a name: abc
Enter a name: def
Enter a name: def
Found It
Done
Is that the supposed behavior?
ahh that is explain wy my antivirus was saying to me that he is sespecting in my c file. does make sense
think so xd
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?
oh
gonna check 1sec
you can try it in your browser inside onlinegdb.com tool
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");
}
i dont understand wy bec i already now that variables in c already have a default value right?
in place of strcmp?
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
Common Weakness Enumeration (CWE) is a list of software weaknesses.
Common Weakness Enumeration (CWE) is a list of software weaknesses.
Common Weakness Enumeration (CWE) is a list of software weaknesses.
Common Weakness Enumeration (CWE) is a list of software weaknesses.
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.
like wt is the diff between C# and C??!
WOW!
well i think i got it now
thank you very much for ur explanation