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

Should I learn c before c++?

0 Answers  


Explain the difference between the local variable and global variable in c?

0 Answers  


CopyBits(x,p,n,y) copy n LSBs from y to x starting LSB at 'p'th position.

9 Answers   Adobe,


plz answer..... a program that reads non-negative integer and computes and prints its factorial

2 Answers  


Can you please compare array with pointer?

0 Answers  






what defference between c and c++ ?

6 Answers  


What is difference between static and global variable in c?

0 Answers  


in multiple branching construct "default" case is a) optional b) compulsarily c) it is not include in this construct d) none of the above

0 Answers  


1. Write a c pgm to print 1 to 100 without using loops. 2. Write a c pgm for leap year 3. Write a c pgm fibbonacci series,factorial 4. Write a c pgm count no of lines , blanks, tabs in a para(File concept) 5. Write a c pgm to print the letter as per given condition i.e.. if u give 4 out put should b 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 6.how do get the o/p in number from 1 to 100 in the screen without using control statement? 7. who do u print the word "hello world" without using "printf" statement? 8. write sql program to get the detail of student in a class? Definitions: structure union arrays linkedlist macros directives difference b/w pre processorsDiffrence: 1.Constructors and destructors 2.Structure and Union 3.Array and Lists 4.pre processor... 5. Privillages in C++ 6.structure and union 7.break and continue 8.while and dowhile Pgm..

3 Answers  


How can I rethow can I return a sequence of random numbers which dont repeat at all?

0 Answers  


What is the argument of a function in c?

0 Answers  


what will be the output off the following program? #include<stdio.h> int main() { int a; a=015+0*71+5; printf("%d,a"); return0; }

9 Answers   HCL,


Categories