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
Should I learn data structures in c or python?
Differentiate between declaring a variable and defining a variable?
Is c still used?
Why flag is used in c?
Dont ansi function prototypes render lint obsolete?
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.
What is pragma c?
What does char * * argv mean in c?
What is pass by value in c?
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
What is structure packing in c?
What is getch c?
How can I write functions that take a variable number of arguments?
write a progrmm in c language take user interface generate table using for loop?
Is it acceptable to declare/define a variable in a c header?