Finding first/last occurrence of a character in a string
without using strchr( ) /strrchr( ) function.
Answers were Sorted based on User's Feedback
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 |
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 |
What is a program flowchart and how does it help in writing a program?
write program on arrays
What is an lvalue in c?
What is a const pointer?
When we use void main and int main?
How do I determine whether a character is numeric, alphabetic, and so on?
What is #include in c?
What is assert and when would I use it?
pgm in c to reverse string by word using array(god is love becomes love is god) (no additional array can used,space is only delimiter between words )
What is maximum size of array in c?
The number of measuring units from an arbitarary starting point in a record,area,or control block to some other point a) recording pointer b) offset c) branching d) none
every function has return the value?