ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

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.

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

C String Programs »


ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.