Welcome back,
I am here with a new program after a long time. You can read the program below and try to answer i.e find the correct output for the program.
Let's look at the program below ,
You can copy the code from above :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void reshuffleString(char *str) {
int length = strlen(str);
for (int i = 0; i < length - 1; i++) {
int j = i + rand() % (length - i);
char temp = str[i];
str[i] = str[j];
str[j] = temp;
}
}
void reverseString(char *str) {
int length = strlen(str);
for (int i = 0; i < length / 2; i++) {
char temp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = temp;
}
}
void deleteRandomChars(char *str, int numToDelete) {
int length = strlen(str);
if (numToDelete >= length) {
str[0] = '\0'; // Delete the entire string
} else {
for (int i = 0; i < numToDelete; i++) {
int randomIndex = rand() % length;
for (int j = randomIndex; j < length - 1; j++) {
str[j] = str[j + 1];
}
str[length - 1] = '\0';
length--;
}
}
}
int main() {
srand(time(NULL)); // Initialize random seed
char input[100];
printf("Enter a string: ");
fgets(input, sizeof(input), stdin);
input[strcspn(input, "\n")] = '\0'; // Remove the newline character
printf("Options:\n");
printf("1. Reshuffle the string\n");
printf("2. Reverse the string\n");
printf("3. Delete random characters\n");
int option;
printf("Select an option (1/2/3): ");
scanf("%d", &option);
switch (option) {
case 1:
reshuffleString(input);
break;
case 2:
reverseString(input);
break;
case 3:
int numToDelete;
printf("Enter the number of characters to delete: ");
scanf("%d", &numToDelete);
deleteRandomChars(input, numToDelete);
break;
default:
printf("Invalid option\n");
return 1;
}
printf("Modified string: %s\n", input);
return 0;
}
I think most of you will know what the program is doing and it has some simple strings operation that are being performed.
Try and find the output for the program shared and write it in the comments below.
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.