#: 4363 S3/Languages
14-Jun-90 08:27:10
Sb: #4359-#help with 'c' index()
Fm: Jeff Dege 76426,211
To: SCOTT HOWELL 70270,641
What you need to keep in mind is that a string is an array of characters,
and the name of array variable is treated as a pointer to the first elemnt.
Once you have a pointer to the first 'm' in a string, all you have to do is
subtract to get the index.
#include
#include
main()
{
char string[80], *p;
puts("Enter a string");
fflush(stdin); /* flush the output buffer */
gets(buffer);
p = Index(string, m);
printf("The first 'm' occurs in location %d\n", p - string);
}
If you'll notice, if the string starts with an 'm', this program will print
0 (all C arrays start at 0.)
There is 1 Reply.
|