Q:

C program to print indexes of a particular character in a string

belongs to collection: C String Programs

0

C program to print indexes of a particular character in a string

In this program we will read a string and print indexes of a particular character in C language, for example, if input string is "Hi there, how are you?" and we want to print the indexes of character ‘o’ then program will return 11 and 19, because ‘o’ exists on both indexes.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program to get the indexes of a particular characters in C

#include <stdio.h>

int main()
{
	char str[30],ch;
	int  ind[10],loop,j;
	
	printf("Enter string: ");
	scanf("%[^\n]s",str);
	
	printf("Enter character: ");
	getchar();
	ch=getchar();
	
	j=0;
	for(loop=0; str[loop]!='\0'; loop++)
	{
		if(str[loop]==ch)
			ind[j++]=loop;
	}
	
	printf("Input string is: %s\n",str);
	printf("Indexes: ");
	for(loop=0; loop<j; loop++)
		printf("%d \t",ind[loop]);

	return 0;
}

Output

Enter string: Hi there, how are you?
Enter character: o
Input string is: Hi there, how are you? 
Indexes: 11	 19

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
C program to capitalize first character of each wo... >>
<< C program to compare two strings using pointers...