C Program To Trim a String

C Program To Trim a String: Suitable examples and sample programs have also been added so that you can understand the whole thing very clearly. The compiler has also been added with which you can execute it yourself.

C Program To Trim a String

#include <stdio.h>
#include <string.h>
int main() {
char str[256];
int start, end, i;
printf("Enter your input string:");
fgets(str, 256, stdin);
str[strlen(str) - 1] = '
#include <stdio.h>
#include <string.h>
int main() {
char str[256];
int start, end, i;
printf("Enter your input string:");
fgets(str, 256, stdin);
str[strlen(str) - 1] = '\0';
printf("Start & End Point to trim(0-%d):\n", strlen(str) - 1);
scanf("%d%d", &start, &end);
if (start > end || start < 0 || end > strlen(str) - 1) {
printf("Boundary Value Exceeded!!\n");
return 0;
}
printf("Resultant String:\n");
for (i = 0; i < start; i++) {
printf("%c", str[i]);
}
for (i = end + 1; i < strlen(str); i++) {
printf("%c", str[i]);
}
printf("\n");
return 0;
}
'; printf("Start & End Point to trim(0-%d):\n", strlen(str) - 1); scanf("%d%d", &start, &end); if (start > end || start < 0 || end > strlen(str) - 1) { printf("Boundary Value Exceeded!!\n"); return 0; } printf("Resultant String:\n"); for (i = 0; i < start; i++) { printf("%c", str[i]); } for (i = end + 1; i < strlen(str); i++) { printf("%c", str[i]); } printf("\n"); return 0; }

Output Of Program

Enter your input string:56
Start & End Point to trim(0-1):
7
8
Boundary Value Exceeded!!