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 structure pointer?

0 Answers   Accenture, HCL,


what is the output of the below code? main( ) { printf ( "\nOnly stupids use C?" ) ; display( ) ; } display( ) { printf ( "\nFools too use C!" ) ; main( ) ; }

3 Answers  


why do some people write if(0 == x) instead of if(x == 0)?

0 Answers  


What is wild pointer in c?

0 Answers  


Write a program that receives as input a number omaadel-n-print, four digits.

0 Answers  


What is the difference function call by value & function call by reference?

6 Answers  


How can I convert integers to binary or hexadecimal?

2 Answers  


Given a string write a program to print all alphabetical characters in the order of their occurance first,followed by the sum of the numeric characters then followed by the special characters in the order of their occurance.

1 Answers   College School Exams Tests, Wipro,


Write a C program to perform some of the operation which can be performed using Single linked list

1 Answers   Qualcomm,


long int size a) 4 bytes b) 2 bytes c) compiler dependent d) 8 bytes

18 Answers   Acropolis, HCL, Intel, TCS,


What is the difference between union and anonymous union?

0 Answers   Hexaware,


How the c program is executed?

0 Answers  


Categories