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
Subtract Two Number Without Using Subtraction Operator
What 'lex' does?
How can you find the day of the week given the date?
What are structure members?
console I/O functions means a) the I/O operations done on disk b) the I/O operations done in all parts c) the input given through keyboard is displayed VDU screen d) none of the above
Can you assign a different address to an array tag?
How do you view the path?
What is the function of this pointer?
Is calloc better than malloc?
Describe the order of precedence with regards to operators in C.
What are void pointers in c?
Why is structure important for a child?
.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }
Is there anything like an ifdef for typedefs?
What is string concatenation in c?