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

Answers were Sorted based on User's Feedback



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

Answer / sepideh

#include<iostream>
#include<conio.h>
using namespace std;
int main(){
char m[20];
char x;
gets(m);
x=getch();
for(int i=0;i!=null;i++)
if(m[i]==x){
cout<<i;}
getch();}

Is This Answer Correct ?    9 Yes 0 No

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

Answer / 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

More C Interview Questions

main() { float f1=10.5; double db1=10.5 if(f1==db1) printf("a"); else printf("b") }

2 Answers   CSC,


main() { int i=5; printf("%d%d%d%d",i++,i--,i); }

10 Answers  


Write a programm such that if user enter 11.25 it roundup to 11 but if user enter 11.51 upto 11.99 it will round up to 12 i.e.;convert the floting point value into integer format as explain above..

2 Answers  


Between macros and functions,which is better to use and why?

0 Answers  


What happens if you free a pointer twice?

0 Answers  






actually i have 2 years teaching experience as computer faculty but now i am a DBA but when i go for interview many peoples asked me why i left my teaching profession and why i want to come in this field kindly give me the proper answer of this queston

3 Answers   Ramco,


Reverse the bit order in a single macro. eg. i/p = 10010101 --> o/p = 10101001

2 Answers  


What is the advantage of using #define to declare a constant?

0 Answers   Agilent, ZS Associates,


How can a string be converted to a number?

0 Answers  


what are the different storage classes in c?

0 Answers   TCS,


write a program to sum of its digit with using control structure or with out using loop. for ex: let the number is 25634 then answer will be=2+5+6+3+4=20

4 Answers  


Why functions are used in c?

0 Answers  


Categories