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
Why does not c have an exponentiation operator?
Place the #include statement must be written in the program?
Explain how do you generate random numbers in c?
What are run-time errors?
how to capitalise first letter of each word in a given string?
What are register variables? What are the advantage of using register variables?
Explain continue keyword in c
What is difference between structure and union with example?
I heard that you have to include stdio.h before calling printf. Why?
Why c is a mother language?
What is c basic?
What is the use of getchar() function?
What is the size of empty structure in c?
Is fortran faster than c?
Where does the name "C" come from, anyway?