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 the advantage of a random access file?
How many loops are there in c?
Is swift based on c?
Explain the difference between structs and unions in c?
What are reserved words?
What are linker error?
what is the difference between c and java?
write a program to copy the string using switch case?
Write down the program to sort the array.
What is putchar() function?
Give the Output : * * * * * * * * * *
What is the difference between void main and main in c?