-
Counting characters
Hi
I want to know how to count the number of lower-case letters that ocurres only once in a string.
the string is created by the user, i don't want to count the letters from a file.
example:
program output: insert a string: abscdecde356YG
the number os lower-case letters that ocurres only once is: 3(a, b and s)
thanks, Calavera
-
One way to do this would be to create an array that represents all the characters in the alphabet and to initialize the contents to 0. You can then increment the contents by 1 the first time a lowercase letter is found. That way when you're done examining the string, just print out the letters that have 1 as a value. For the string itself, you could go through it in a loop and use islower() to check if the character is lowercase.
-
Does anyone around here ever think for themselves? Come on! Give it some thought. How do you think you'd do it? Break it into steps. I'm tired of seeing everyone just show up and say "do this for me", or "how can i...". Give up programming if you aren't going to think for yourself, because if you don't, you have no chance of ever becoming a programmer.
Read the announcements while you're at it, so I don't have to tell you all the rest of the rules around here as a reply to your second post.
Quzah.
-
It seems this is homework... Anyway:
Code:
unsigned int
unique_lower(const char *const string)
{
const char *pointer;
unsigned int count = 0U;
for (pointer = string; *pointer != '\0'; pointer++)
if (islower(*pointer) && strchr(string, *pointer) == strrchr(string, *pointer))
count++;
return count;
}
-
ha ha@previous post!!...the teacher will know for sure that you cheated
-
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...