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

Subtract Two Number Without Using Subtraction Operator

852


What 'lex' does?

1184


How can you find the day of the week given the date?

1204


What are structure members?

1094


console I/O functions means a) the I/O operations done on disk b) the I/O operations done in all parts c) the input given through keyboard is displayed VDU screen d) none of the above

1179


Can you assign a different address to an array tag?

1175


How do you view the path?

1128


What is the function of this pointer?

1345


Is calloc better than malloc?

1023


Describe the order of precedence with regards to operators in C.

1047


What are void pointers in c?

997


Why is structure important for a child?

1105


.main() { char *p = "hello world!"; p[0] = 'H'; printf("%s",p); }

1301


Is there anything like an ifdef for typedefs?

1154


What is string concatenation in c?

1094