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

What is the advantage of a random access file?

0 Answers  


How many loops are there in c?

0 Answers  


Is swift based on c?

0 Answers  


Explain the difference between structs and unions in c?

0 Answers  


What are reserved words?

0 Answers  






What are linker error?

0 Answers  


what is the difference between c and java?

1 Answers  


write a program to copy the string using switch case?

0 Answers   Mind Tree,


Write down the program to sort the array.

4 Answers   Impiger,


What is putchar() function?

0 Answers  


Give the Output : * * * * * * * * * *

2 Answers  


What is the difference between void main and main in c?

0 Answers  


Categories