compare more than two strings - c

I have 5 strings. I need to compere all five at once.
char set_password1[5] = "1111";
char set_password2[5] = "2222";
char set_password3[5] = "3333";
char set_password4[5] = "4444";
char set_password5[5] = "5555";
if(!strcmp(Entered_Password,set_password1))
{
}
If any of these passwords match with Enter _Password i need to do something. so do i have to write five if statements like this
if(!strcmp(Entered_Password,set_password1))
{
}
if(!strcmp(Entered_Password,set_password2))
{
}
if(!strcmp(Entered_Password,set_password3))
{
}
if(!strcmp(Entered_Password,set_password4))
{
}
if(!strcmp(Entered_Password,set_password5))
{
}
or is there any other way.
I already tried this way, but it didn't work.
if(!strcmp(Entered_Password, (set_password1||set_password2||set_password3||set_password4||set_password5))
{
}

Alternatively, you may use a 2D array.
char password[5][5] = {"1111", "22222", "3333", "44444", "55555"};
int match = 0;
for(int i = 0; i < 5; i++)
{
if(strcmp(password[i], user_password) == 0)
{
match = 1;
break;
}
}
if(match == 1)
{
//do your action
}

Replace line
if(!strcmp(Entered_Password, (set_password1||set_password2||set_password3||set_password4||set_password5))
with
if (!(strcmp(Entered_Password, set_password1) && strcmp(Entered_Password,set_password2) && (strcmp(Entered_Password,set_password3) && strcmp(Entered_Password,set_password4) && strcmp(Entered_Password,set_password5)))
You have to compare each variable seperately with the initial string.

Related

Format a string to specific format

//take the buffer, format it into a **write test.c 4:[this is what I want written in the file]**
void saveFile(Request request) {
//user inputted save test.c 4:[this is what I want written in the file]
char command[5] = "write";
for (int i = 0; i < 5; ++i) {
request[i] = command[i];
}
}
The issue is my logic will format the string into writetest.c 4:[this is what I want written in the file]
I want to add space between write and the next char
I solved the issue :)
void saveFile(char * request) {
//user inputted save test.c 4:[this is what I want written in the file]
char command[5] = "write";
char copyWithExtraBytes[1000];
strcpy_s(copyWithExtraBytes,strlen(request)+1, request);
for (int i = 0; i < strlen(copyWithExtraBytes); ++i) {
copyWithExtraBytes[i] = command[i];
if (i == 4)
{
break;
}
}
for (int i = 5; i < (strlen(copyWithExtraBytes))+1; ++i) {
if (i ==5)
{
copyWithExtraBytes[5] = ' ';
continue;
} else
{
copyWithExtraBytes[i] = request[i -1];
if (i == strlen(request))
{
copyWithExtraBytes[i+1] = request[i-1];
}
}
}
printf("%s",copyWithExtraBytes);
}

how can i assign a randomly generated integer to a string in C?

i'm trying to make a slot machine type thing and i wanted to assign the randomly generated numbers to certain symbols like 1 = cherry, 2 = bell and so on so i could print out the results in symbol form at the end.
i tried putting the symbols as strings in an array and assigning the numbers to the elements in the array in each slot functions but it didn't work out... is there a way to do this?
here's the code i've written so far, minus the array attempts. any suggestions would be helpful! :D
EDIT: here's an example of what i've tried doing on one of the slots but it keeps saying i need a cast to assign the integer from a pointer (i've tried searching online but idk how to do this)
char * slotOne(int randOne, const char *symbols[])
{
randOne = rand() % 4 + 1;
if (randOne = 1)
{
randOne = *symbols;
}
if (randOne = 2)
{
randOne = *(symbols+1);
}
if (randOne = 3)
{
randOne = *(symbols+2);
}
else
{
randOne = *(symbols+3);
}
return randOne;
}
this is the part of my main function where i've tried declaring the string array:
int main()
{
int x, one, two, three;
const char *symbols[4] = {"bell", "orange", "cherry", "horseshoe"};
srand(time(NULL));
one = slotOne(x);
two = slotTwo(x);
three = slotThree(x);
printf("%s - %s - %s\n", one, two, three);
//...
}
not sure if %s or %c is the right type too...
At least these problems:
Code is assigning = when it should compare ==.
// if (randOne = 1)
if (randOne == 1)
The last if () { ... } else { ... } will cause one of the 2 blocks to execute. OP wants an if () { ... } else if () { ... } else { ... } tree.
// Problem code
if (randOne = 3) {
randOne = *(symbols+2);
} else {
randOne = *(symbols+3);
}
Suggest
if (randOne == 1) {
randOne = *symbols;
} else if (randOne == 2) {
randOne = *(symbols+1);
} else if (randOne == 3) {
randOne = *(symbols+2);
} else {
randOne = *(symbols+3);
}
Also research switch.
switch (randOne) {
case 1:
randOne = *symbols;
break;
case 2:
randOne = *(symbols+1);
break;
case 3:
randOne = *(symbols+2);
break;
default:
randOne = *(symbols+3);
break;
}
Or consider a coded solution:
randOne = *(symbols+(randOne-1));
But code needs to return a pointer to a string not an int and has no need to pass in randOne as a parameter.
const char * slotOne(const char *symbols[]) {
int randOne = rand() % 4;
return symbols[randOne];
}
Calling code also needs to adjust to receive a const char *, not an int.
// int one;
// one = slotOne(x);
const char *one = slotOne(symbols);

How to check function return value in an if statement without calling it

I'm trying to check the return value of the function in an if statement so that I will return its value if its 1, but it gets called when I do it in an if statement. Is there a way where I prevent the call in the if statement. I want to check all three function but if I checked one without if statement then it returns 0 and stops.
if (argc != 2)
{
printf("Usage: ./substitution key\n");
return 1;
}
else
{
string chars = argv[1];
if (check_length(chars) == 1)
{
return check_length(chars);
}
else if (check_rc(chars) == 1)
{
return check_rc(chars);
}
else if (check_alpha(chars) == 1)
{
return check_alpha(chars);
}
}
string ptext;
ptext = get_string("Input Text: \n");
}
int check_rc(string chars)
{
for (int i = 0, n = strlen(chars); i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (!(i == j))
{
if (chars[i] == chars[j])
{
printf("Key must not contain repeated characters. \n");
return 1;
}
}
}
}
return 0;
}
int check_alpha(string chars)
{
for (int i = 0, n = strlen(chars); i < n; i++)
{
if (isdigit(chars[i]))
{
printf("Key must only contain alphabetic characters. \n");
return 1;
}
}
return 0;
}
int check_length(string chars)
{
int charLength = strlen(chars);
if( charLength !=26)
{
printf("Key must contain 26 characters.\n");
return 1;
}
return 0;
}
Your Question is hard to understand but i am assuming that if you want to reduce theamount of unnessecary check you could apply the concept of short-circuit evalution of &&
or u could just not use (else if). and use 3 if statement instead.
all following(else if) is skip if one of the if statement before it is true.
hope this help.
You can save the return value in a local variable and use it all the way:
int check_rc_val = check_length(chars);
if (check_rc_val == X)
{
return check_rc_val;
}
int check_rc_val = check_rc(chars);
if (check_rc_val == Y)
{
return check_rc_val;
}
int check_alpha_val = check_alpha(chars);
if (check_alpha_val == Z)
{
return check_alpha_val;
}

Typescript sorting array with objects on multiple properties

I like to sort an array with objects that have multiple properties. My objects have a string called name and a boolean called mandatory.
First i want to sort on age, next on the name.
How do I do this?
Ordering by age is easy...:
this.model.mylist.sort((obj1: IObj, obj2: IObj => {
if (obj1.age < obj2.age) {
return -1;
}
if (obj1.age > obj2.age) {
return 1;
}
return 0;
});
Well, you only add comparation for case when the two age values are the same. So something like this should work:
this.model.mylist.sort((obj1: IObj, obj2: IObj) => {
if (obj1.age < obj2.age) {
return -1;
}
if (obj1.age > obj2.age) {
return 1;
}
return obj1.name.localeCompare(obj2.name);
});
Something like this should work. The method compares the current and next values and adds comparison to the case when the two age values are the same. Then assume the column name in order based on age.
private compareTo(val1, val2, typeField) {
let result = 0;
if (typeField == "ftDate") {
result = val1 - val2;
} else {
if (val1 < val2) {
result = - 1;
} else if (val1 > val2) {
result = 1;
} else {
result = 0;
}
}
return result;
}
-
this.model.mylist.sort((a, b) => {
let cols = ["age", "name"];
let i = 0, result = 0, resultordem = 0;
while (result === 0 && i < cols.length) {
let col = cols[i];
let valcol1 = a[col];
let valcol2 = b[col];
let typeField = "string";
if (col == "age") {
typeField = "number";
}
if (valcol1 != "null" && valcol1 != "null") {
resultordem = this.compareTo(valcol1, valcol2, typeField);
if (resultordem != 0) {
break;
}
}
i++;
}
return resultordem;
});

Accessing certain elements in an Struct in C

So I have the following struct I created:
struct _I_TypeInstructions {
const char *instructionName;
char *opcode;
} I_TypeInstructions[] = { { "lw", "100011" }, { "sw", "101011" }, { "beq",
"000100" } };
typedef struct _I_TypeInstructions I_TypeInstructionsStruct;
If I have a new instructionName and I want to check if it is in the I_TypeInstructionsStruct how do I iterate through just the *instructionName part of the struct above. For example the function I want to write would look something like
bool checkIfInstructionIsI_Type(char *instructionName) {
// somehow iterate through instructionNames in the struct above
// checking if parameter char *instructionName in this method is equal to
// "lw" "sw" "beq" but skipping over the binary numbers.
}
Searching a list of structs is rather straight forward:
bool checkIfInstructionIsI_Type(char *instructionName)
{
for (int i = 0; i<NumInstructions; i++)
{
if (strcmp(I_TypeInstructions[i].instructionName, instructionName) == 0)
{
return true;
}
}
return false;
}
int i;
for(i = 0; i < 3; ++i)
{
if (strcmp(instructions[i].instructionName, instructionName) == 0)
{
printf("Match found");
}
}
It's generally more useful to return the actual element that matches your string. It's the same amount of work anyway.
Add an empty element to the end of your array and then you have a end marker.
typedef struct _I_TypeInstructions {
const char *instructionName;
char *opcode;
} I_TypeInstructionsStruct;
I_TypeInstructionsStruct I_TypeInstructions[] = {
{ "lw", "100011" },
{ "sw", "101011" },
{ "beq", "000100" },
{ 0, 0}
};
I_TypeInstructionsStruct *find_instruction(char *name)
{
I_TypeInstructionsStruct *i ;
for (i = I_TypeInstructions ; i->instructionName ; i++)
if (!strcmp(i->instructionName,name)) return i ;
return 0 ;
}

Resources