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
Compare interpreters and compilers.
What is the use of a conditional inclusion statement in C?
Explain argument and its types.
what are the advanced features of functions a) function declaration and prototypes b) calling functions by value or by reference c) recursion d) all the above
When should you not use a type cast?
Which is best book for data structures in c?
Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary?
Is that possible to store 32768 in an int data type variable?
application areas a 'c' a) operating system b) graphics, interpreter, assembler c) program evalution, communication softwares d) all the above
what is the role you expect in software industry?
which type of aspect you want from the student.
How can I recover the file name given an open stream?
what is the difference between 123 and 0123 in c?
formula to convert 2500mmh2o into m3/hr
What is structure in c definition?