C Program : Explanation to Some String operation program

in c-programming •  last year 

Welcome back,
I am here with a the explanation for our new program shared yesterday after a long time. If you want to skip the explanation for now then visit the blog i shared yesterday.

Let's look at the program below ,

Screenshot 2023-08-13 205348.png

Screenshot 2023-08-13 205523.png

image.png

image.png

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

Here is the explanation :

Include Header Files:

Function Definitions

These functions are responsible for performing the reshuffle, reverse, and delete operations on the input string.

Reshuffle String Function

This function takes a string as input and shuffles its characters randomly.

Delete Random Characters Function

This function deletes a specified number of random characters from the input string.

Main Function

The main function is where the program execution begins. It initializes the random number generator seed and takes user input for the string.

The fgets function reads the input string from the user and stores it in the input array. The strcspn function is used to remove the trailing newline character from the input.

Display Options

Select Option

Option Handling

Depending on the selected option, the program calls the corresponding function to perform the desired operation on the input string. If an invalid option is chosen, the program displays an error message and exits

Display Modified String

The program returns 0 to indicate successful execution.

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!