-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSlicer.c
More file actions
33 lines (27 loc) · 784 Bytes
/
StringSlicer.c
File metadata and controls
33 lines (27 loc) · 784 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<stdio.h>
int main(){
//Taking input of the sentence and the indecies to be extracted
int s = -1, e = -1;
char str[255];
printf("Input a sentence: ");
fgets(str, 255, stdin);
do{
printf("\nInput the start index to slice: ");
scanf("%d", &s);
printf("\nInput the end index to slice: ");
scanf("%d", &e);
if(s > e || e > strlen(str) || s < 0)
printf("\nInvalid Input! Input again.\n");
}while(s > e || e > strlen(str) || s < 0);
//Slicing up the string to get a substring
char sub[255];
int k = 0;
for(int j = s; j < e+1; j++){
sub[k] = str[j];
k++;
}
sub[k] = '\0';
//Printing the substring
printf("\nThe substring is: %s\n", sub);
return 0;
}