since it already has been spoiled , here is a similar code to rmps's but in function 'main()' :
Code:
#include <stdio.h>
#include <ctype.h> /* for function 'islower()' */
#include <string.h> /* for 'strchr()' and 'strrchr()' */
int main()
{
char string[100];
char *p;
int count = 0;
fgets(string, sizeof(string), stdin);
for(p = string ; *p != '\0' ; p++)
if(islower(*p) && (strchr(string, *p) == strrchr(string , *p)))
count++;
printf("unique lower-case letters occured %d times in the string\n\n\n", count);
system("PAUSE");
return 0;
}
ill explain it this time hopefully you'll learn something instead of just copying\pasting the code...
as you see... first we declare the string and the pointer 'p' we'd be using to point to the elements of the strings.
'count' is just a counter to see how many low-case character , that didn't occur twice , are there. Its declared INT because the string wouldn't hold more than 99 chars (including the '\0') , and the alphabet letters are 26 anyway so there is now way its gonna hold more than an INT would allow. (AFAIK)
Then it'll ask the user to input a something into that string with 'fgets()'.
Code:
for(p = string ; *p != '\0' ; p++)
if(islower(*p) && (strchr(string, *p) == strrchr(string , *p)))
count++;
this part would be a bit tough if you're not familiar with pointers.
the for loop has 3 expressions. The first says that the pointer 'p' would be pointing to the first element of 'string' (string equals string[0]) , third will let the pointer 'p' move on to the next element in each loop , second will ensure that it'll keep pointing to the next element untill it reaches the end of the string wich is the string terminator '\0'.
now for the IF statement...
the IF statement here is divided into two expressions... first checks if the character pointed to by 'p' is lower case or not. Second uses strchr() and strrchr() functions that scans our 'string' for the first occurance of a given character. The differences between strchr() and strrchr() that the first scans the string forward and the second scans it backward.
The reason we are comparing the result of these opposite functions is lets say we entered 'aaab' as an input, and *p was pointing to the first 'a' in the string , strchr() would quickly detect the first 'a' and return a pointer to its location , whereas strrchr() would scan the string backward and detect the third 'a' in the string wich doesn't equal the location of the first 'a' thus breaking IF and looping again.
When it loops few times and our pointer points to 'b' in the string , strchr() will find its first occurance in string[3] and strrchr() would , too . That makes our IF statement true because 'b' is a lower-case character AND (&&) both scanning functions point to the same location in the string therefore the counter goes up by one.
and finally the result would be printed with printf and the program terminates successfly returning 0.
hope this helps...