Finding first/last occurrence of a character in a string
without using strchr( ) /strrchr( ) function.

Answer Posted / daniel

#include <stdio.h>
#include <string.h>

int main()
{
char *string = "This is a simple string";
char x = 's';
int i;
int focc, locc;

for (i=0;i<strlen(string);i++){
if (string[i] == x){
focc = i;
break;
}
}

for (i=0;i<strlen(string);i++){
if (string[i] == x)
locc = i;
}

printf ("First occurrence %d, last occurrence %d\n", focc, locc);
return 0;
}

Is This Answer Correct ?    8 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Should I learn data structures in c or python?

748


Differentiate between declaring a variable and defining a variable?

806


Is c still used?

784


Why flag is used in c?

841


Dont ansi function prototypes render lint obsolete?

803


a number whose only prime factors are 2,3,5, and 7 is call humble number,,write a program to find and display the nth element in this sequence.. sample input : 2,3,4,11,12,13, and 100.. sample output : the 2nd humble number is 2,the 3rd humble number is 3,the 4th humble number is ,the 11th humble number is 12, the 12th humble number is 14, the 13th humble number is 15, the 100th humble number is 450.

4790


What is pragma c?

842


What does char * * argv mean in c?

797


What is pass by value in c?

767


Given below are three different ways to print the character for ASCII code 88. Which is the correct way1) char c = 88; cout << c << " ";2) cout.put(88);3) cout << char(88) << " "; a) 1 b) 2 c) 3 d) constant

893


What is structure packing in c?

808


What is getch c?

1041


How can I write functions that take a variable number of arguments?

837


write a progrmm in c language take user interface generate table using for loop?

1782


Is it acceptable to declare/define a variable in a c header?

859