Question: I need to modify below code to have output like: Example: RBGY [ You have 1 white flag and you have 1 red flag] Example:
I need to modify below code to have output like: Example: RBGY [ You have 1 white flag and you have 1 red flag]
Example: CCGG
[ You have 0 white flag and you have 1 red flag]
==================================
#include#include #include #include #define SIZE_STRING 4 #define SIZE_STRING_LONG 15 int main(int argc, char *argv[]){ if(argc != 2) { printf("Usage: ./main "); exit(0); } char szUser[SIZE_STRING_LONG]; char *szComputer = argv[1]; int counter=0; int colour=0; int position=0; char alreadyChecked[SIZE_STRING]; printf(" You have 8 changes to get it right. "); printf(" Computer choses %s", szComputer); //check if the program works allright do{ counter++; position=0; colour=0; memset(alreadyChecked, 0, sizeof(alreadyChecked)); printf(" =================================================== "); printf(" Chance %d", counter); printf(" Please enter your 4 colours choice"); printf(" (Please write capital letters without space "); scanf("%s",szUser); if(strlen(szUser) != SIZE_STRING) { printf(" Sorry you choose a wrong option."); counter--; } else { if(strcmp(szUser,szComputer)) { printf(" Wrong choice. Try again..."); for (int u = 0; u < SIZE_STRING; u++) { if (szUser[u] == szComputer[u]) { position++; } } if (position > 0 && position < 2) { printf(" You have %d red flag", position); } if (position > 1 ) { printf(" You have %d red flags", position); } for (int u = 0; u < SIZE_STRING; u++) { if (szUser[u] == szComputer[u]) { continue; } for (int c = 0; c < SIZE_STRING; c++) { if (!alreadyChecked[c] && szUser[u] == szComputer[c]) { colour++; alreadyChecked[c] = 1; break; } } } if (colour > 0 && colour < 2) { printf(" You have %d white flag", colour); } if (colour > 1 ) { printf(" You have %d white flags", colour); } if (colour == 0 && position == 0) { printf(" 0 white flags and 0 red flags"); } } } }while(strcmp(szUser,szComputer) && counter <=7); if (strcmp(szUser,szComputer)) { printf(" Sorry, you run out of chances..."); } if(!strcmp(szUser,szComputer)) printf(" Right combination ! "); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
