- NEW!
Help answer this question below.
In what? In a string? Its very easy:
...
...
#include <string.h>
...
...
int main(int argc, int *argv[])
{
int a;
char s[80];
...
...
a=strlen(s);
...
...
The strlen function:
int strlen(const char *a)
defined in string.h counts the number of characters within the string pointed to by a and returns the value. The null character at the end is not counted.
If you are working in ASCII characters then you can do something like this:
int main(char *str)
{
int values = 0;
int check[256];
check[' '] = 1;
check['0'] = 1;
...
while(str)
values += check[*(str++)];
return values;
}
I need a simple syntex code for duplicate elimination in C
by mrwigglesjean on September 2nd, 2010
| 1 person likes this
draw a flowchart to find the greatest of three numbers?
by mounica on October 21st, 2010
| 1 person likes this
Problems with dynamic allocation.
by POP Fan on August 31st, 2010
| 1 person likes this
Anybody want to play C Programming Roulette?
by POP Fan on August 18th, 2010
| 1 person likes this
What's a good algorithm for performing Quadratic Regression on a set of points?
by POP Fan on September 7th, 2010
| 1 person likes this
You're reading Can any body help to write a program in c to count no.of char ,spaces,digitsand other
Comments
what about the counting spaces and digits?
by yeroco on August 17th, 2009
It include spaces and digits. But if you want to check specifically for spaces, you can you the function: isspace().
I'm not 100% rue of the prototype, but you could do something like this:
#include <stdio.h>
#include <stdlib.b>
#include <string.h>
#include <ctype.h>
int main()
{
char s[255];
register int a, b;
printf("Type in a string.\n");
gets(s);
for(a=0; a<(strlen(s)); a++)
{
if(isspace(s[a])) b++;
}
printf("\nThere are %d spaces in the string.", b);
}
To count digits, there is a function (isdigit or isnum), and to check for alphabetic caracters you can use isalpha. These funcions all use ctype.h
by POP Fan on August 17th, 2009
Nice.
by yeroco on August 17th, 2009