Reputation: 73
I tried arrays, pointers with string in the following program, but I don't know where I am going wrong.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// finding out the uper case and lower case in a string
int main()
{
char *arr_1[] = {"CalCulaTor", "DoveTail", "MachiNing"};
int i = 0, j;
while(*(arr_1 + i) != '\0')
{
printf(" %d letter is %s ",i,islower(*(arr_1 + i)) ? "Lower case " : "Not lower case");
printf("\n");
i++;
}
system("PAUSE");
return 0;
}
Upvotes: 0
Views: 551
Reputation: 571
char *arr_1[]
is an array of pointers to char.
You have to loop through your array of strings then loop over that string to get each individual element.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char *arr_1[] = {"CalCulaTor", "DoveTail", "MachiNing"};
size_t i, j;
for (i = 0; i < 3; i++) {
puts(arr_1[i]);
for (j = 0; arr_1[i][j]; j++) {
printf("'%d' element is %s.\n", j, islower(arr_1[i][j]) ? "Lowercase" : "Uppercase");
}
putchar('\n');
}
return 0;
}
The first loop will get your string (which is a pointer to char) as arr_1[i]
then looping over that with another variable will give you an element in that pointer to char as arr_1[i][j]
.
Upvotes: 0
Reputation: 121799
A common idiom for an array of strings is to terminate the array with a NULL. For example:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
// finding out the uper case and lower case in a string
char *arr_1[] = {
"CalCulaTor", "DoveTail", "MachiNing", NULL
};
int main()
{
char **a = arr_1;
while(*a)
{
int i = 0;
char *c = *a;
printf ("%s:\n", c);
while (*c) {
printf(" %d letter(%c) is %s ",
i++,
*c,
(islower(*c) ? "Lower case " : "Not lower case"));
c++;
}
printf("\n");
a++;
}
system("PAUSE");
return 0;
}
Upvotes: 0
Reputation: 225052
Your code looks OK to handle a single string, but you have an array of three strings. You need to add another loop to handle the iteration over that array.
Upvotes: 3