C Program : Let us Look at the output for the program shared on 02/08/2023

in c-programming •  last year 

Welcome back,

I am back with the output for the program shared two days back. If you want to skip the output part and explanation part as well then visit the blog shared on 2/03/2023.

Let us look at the program ,

image.png

image.png

image.png

image.png

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 :

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

#define MAX_CRYPTO 5
#define MAX_NAME_LENGTH 10

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;
}

Now as explanation was already shared in the previous blog we will directly move on towards the output part.

Let us look at the output of this program.

image.png

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.

Source

I am happy to call myself a trader and small programmer at the same time now.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!