Write a C program to trim a string.
#include <stdio.h>
#include <string.h>
int main() {
char str[256];
int start, end, i;
/* get the input string from the user */
printf("Enter your input string:");
fgets(str, 256, stdin);
str[strlen(str) - 1] = '\0';
/* get the portion to trim */
printf("Start & End Point to trim(0-%d):\n", strlen(str) - 1);
scanf("%d%d", &start, &end);
/* boundary check */
if (start > end || start < 0 || end > strlen(str) - 1) {
printf("Boundary Value Exceeded!!\n");
return 0;
}
/* Trim the given range of characters and print remaining string */
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;
}
#include <string.h>
int main() {
char str[256];
int start, end, i;
/* get the input string from the user */
printf("Enter your input string:");
fgets(str, 256, stdin);
str[strlen(str) - 1] = '\0';
/* get the portion to trim */
printf("Start & End Point to trim(0-%d):\n", strlen(str) - 1);
scanf("%d%d", &start, &end);
/* boundary check */
if (start > end || start < 0 || end > strlen(str) - 1) {
printf("Boundary Value Exceeded!!\n");
return 0;
}
/* Trim the given range of characters and print remaining string */
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:
jp@jp-VirtualBox:~/$ ./a.out
Enter your input string:hello world
Start & End Point to trim(0-10):
0 5
Resultant String:
world
Enter your input string:hello world
Start & End Point to trim(0-10):
0 5
Resultant String:
world
No comments:
Post a Comment