Traversing in Array of pointers

Please find the below program. the requirement and description of the program also given:

ganesh@ubuntu:~/my_programs/c/letusc/chap9$ cat fa.c.old
/* Program : write a program to count the number of 'e' in thefollowing array of pointers to strings:

	char *s[] = {
			"We will teach you how to..",
			"Move a mountain",
			"Level a building",
			"Erase the past",
			"Make a million",
			"....all through C!"
		    };
*/

/* Date : 09-June-2010 */

# include <stdio.h>
# include <string.h>

int main(void)
{
        char *s[] = {
                        "We will teach you how to..",
                        "Move a mountain",
                        "Level a building",
                        "Erase the past",
                        "Make a million",
                        "....all through C!"
                    };
	int count=0;
	char *sptr;
	sptr=*s;

		while ( *sptr!='\0' ) {
			if (*sptr=='e') { ++count;}
	        	++(sptr);
		}
	printf("count of e's : %d\n", count); 
	return 0;
}

Output of the above program:

ganesh@ubuntu:~/my_programs/c/letusc/chap9$ gcc -o fa fa.c
ganesh@ubuntu:~/my_programs/c/letusc/chap9$ ./fa
count of e's : 2

It only counts the e's in first line. I know I have to traverse through rest of*s[]. But don't know how to do it. Can any one please help me in this?

Thanks in Advance,
Ramkrix

int n;

for(n=0; n<5; n++)
{
  char *str=s[n];
  // count all e's in s
  ...
}

Thanks for the quick reply. I wll use this. Howevr, I have one more question. Here from the declaration of char *s[], we are easily able to note that there 6 "pointer to char arrays" and using a variable as suggested by you, we can traverse. Is there any other way, such that I am not using any variable line "n" to traverse.

I am asking like this:
we have '\0' character to say "end of string" and we use this character to traverse along string like (*sptr!='\0')
Similarly, do we have any thing to notify "end of pointer to character arrays" in Array of pointers.

Thanks,
Ramkrix

You need another char** that points to s...and ensure that the array of char pointers s is null terminated.

Strings NULL-terminate themselves, but nothing else does, so your array is not NULL-terminated; it couldn't know when to stop. And each string is fully independent, the contents of one string won't help you find where the next begins.

If the array was NULL-terminated, you could do so like this:

# include <stdio.h>
# include <string.h>

int main(void)
{
char *s[] = {
"We will teach you how to..",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"....all through C!",
NULL /* Needed to know where to stop! */
};

char **ptr=s;

while( (*ptr) != NULL)
{
  char *str=(*ptr);

  // count e's in str
  ...

  // move to next string
  ptr++;
}

}

Thank you Corona and Shamrock

homework?
you don't need a NULL pointer, though it's one method.
well I can tell you the idiom to correctly go through a char*[]...

# include <stdio.h>
# include <string.h>

int main(void)
{
    char *s[] = {
    "We will teach you how to..",
    "Move a mountain",
    "Level a building",
    "Erase the past",
    "Make a million",
    "....all through C!"
    };

    size_t n = sizeof s/sizeof(char *);
    char **p = s;

    while (n--) {
       puts(*p++);
       // create a counting function here
   }
}

$ ./ramk
.$ ./ramk
We will teach you how to..
Move a mountain
Level a building
Erase the past
Make a million
....all through C!
$ 
# include <stdio.h>
# include <string.h>
int main(void)
{
char *s[] = {
"We will teach you how to..",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"....all through C!"
};
int count=0,n=0,i=0;
clrscr();
while ( n<6 ) {
   if (*((s[n])+i++)=='e') { ++count;}
    else
 if(*((s[n])+i)=='\0'){
  n++;i=0;}
}
printf("count of e's : %d\n",count);
return 0;
}

yes showkat, but you have a magic number for the loop.
i.e. 6.

that is not good practice.
so if you edit the initialiser you have to remember to change the loop number.
my method or the NULL method is the safe way to traverse an array.

yes you are right.. So if I include the statement that you used to get the count of n then combination of you and me will make this program even better- right?

# include <stdio.h>
# include <string.h>
int main(void)
{
char *s[] = {
"We will teach you how to..",
"Move a mountain",
"Level a building",
"Erase the past",
"Make a million",
"....all through C!"
};
int count=0,i=0;
size_t n = (sizeof s/sizeof(char *))-1;
while ( n >=0) {
   if (*((s[n])+i++)=='e') { ++count;}
    else
 if(*((s[n])+i)=='\0'){
  n--;i=0;}
}
printf("count of e's : %d\n",count);
return 0;
}

Code tags please.

So does your posted code and that is not what the o/p wanted...if you see his 2nd post.

My last code makes it generalized- Don't you think so?

er, I fail to see any digits in my posted code!
plus I was only showing a general way to iterate a char**