Welcome back,
I am here with a new program and this time we have involved crypto to make it a interesting one. Though it is a simple program but go ahead and see if you can understand the program.
Let us look at the program ,
There are 'S' and 's' at few places in the above picture and they might have got written while i was taking the screenshot of the written program.
You can copy the code from below :
void printCryptoArray(char crypto[MAX_CRYPTO][MAX_NAME_LENGTH], int numCrypto) {
printf("Current Cryptos: ");
for (int i = 0; i < numCrypto; i++) {
printf("%s ", crypto[i]);
}
printf("\n");
}
int main() {
char crypto[MAX_CRYPTO][MAX_NAME_LENGTH] = {"DOGE", "BTC", "HIVE", "ETH", "LTC"};
int numCrypto = 5;
while (1) {
char action[10];
printf("\nEnter 'add' to add a new crypto, 'remove' to remove a crypto, or 'exit' to quit: ");
scanf("%s", action);
if (strcmp(action, "add") == 0) {
if (numCrypto >= MAX_CRYPTO) {
printf("Maximum number of cryptos reached!\n");
continue;
}
char newCrypto[MAX_NAME_LENGTH];
printf("Enter the name of the new crypto: ");
scanf("%s", newCrypto);
// Check if the new crypto already exists
int cryptoExists = 0;
for (int i = 0; i < numCrypto; i++) {
if (strcmp(crypto[i], newCrypto) == 0) {
cryptoExists = 1;
break;
}
}
if (cryptoExists) {
printf("%s already exists in the list.\n", newCrypto);
} else {
strcpy(crypto[numCrypto], newCrypto);
numCrypto++;
printf("%s has been added to the list.\n", newCrypto);
}
} else if (strcmp(action, "remove") == 0) {
char removeCrypto[MAX_NAME_LENGTH];
printf("Enter the name of the crypto to remove: ");
scanf("%s", removeCrypto);
int foundIndex = -1;
for (int i = 0; i < numCrypto; i++) {
if (strcmp(crypto[i], removeCrypto) == 0) {
foundIndex = i;
break;
}
}
if (foundIndex == -1) {
printf("%s does not exist in the list.\n", removeCrypto);
} else {
for (int i = foundIndex; i < numCrypto - 1; i++) {
strcpy(crypto[i], crypto[i + 1]);
}
numCrypto--;
printf("%s has been removed from the list.\n", removeCrypto);
}
} else if (strcmp(action, "exit") == 0) {
printf("Exiting the program.\n");
break;
} else {
printf("Invalid action. Please try again.\n");
}
printCryptoArray(crypto, numCrypto);
}
return 0;
}
I think most of you will know what the program is doing but to write the output is something different. So go ahead and try to find the output.
I will come here with explanation part tomorrow.
If you also want to learn basics and start your C programming journey then find a teacher today for free on Youtube.
I learnt alot from a teacher who teaches c in hindi and if you are finding someone who teaches in hindi then this person will definitely help you.
I am happy to call myself a trader and small programmer at the same time now.
Happy trading and keep learning what you love.