Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

Finding first/last occurrence of a character in a string
without using strchr( ) /strrchr( ) function.

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Why does not c have an exponentiation operator?

999


Place the #include statement must be written in the program?

993


Explain how do you generate random numbers in c?

1044


What are run-time errors?

1058


how to capitalise first letter of each word in a given string?

1875


What are register variables? What are the advantage of using register variables?

1132


Explain continue keyword in c

965


What is difference between structure and union with example?

992


I heard that you have to include stdio.h before calling printf. Why?

1010


Why c is a mother language?

967


What is c basic?

1127


What is the use of getchar() function?

1076


What is the size of empty structure in c?

1020


Is fortran faster than c?

987


Where does the name "C" come from, anyway?

1081