Answer to Question #294081 in C for Arun

Question #294081

Fetch substring from the given string and display the sub-string starting and ending position. Also, print the substring and its length


Runtime Input :


programming


program


Output :


0 6


Length is 7

1
Expert's answer
2022-02-04T14:49:34-0500


#include <stdio.h>
#include <conio.h>




void main(){
	char inputString[100];
	char inputSubstring[100];
	int i,j;
	int startingPosition=0;
	int endingPosition=0;
	int length=0;


	printf("Enter string: ");
	gets(inputString);
	printf("Enter substring: ");
	gets(inputSubstring);


	for(j=0;inputSubstring[j]!='\0';j++){
		length++;
	}
	for (i = 0, j = 0; inputString[i] != '\0' && inputSubstring[j] != '\0'; i++)
	{
		if (inputString[i] == inputSubstring[j])
		{
			j++;
		}
		else
		{
			j = 0;
		}
	}
	if (j == length){
		startingPosition=i - j;
		endingPosition=startingPosition+length-1;
		printf("%d %d\n",startingPosition,endingPosition);
		printf("Length is %d\n\n",length);
	}else{
		  printf("Substring not found\n\n");
	}






	getch();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS