how can I count a certain letter or digit in a word?
let's say
MISSISSIPPI
1=M
4=I
4=S
2=P
it would me more appreciated if your answer would be string, array, etc.
C Programmers, help me please...?
char *mystring = "MISSISSIPPI";
int length = strlen(mystring);
int countletters(char str[])
{
int count = 0, k = 0;
while (str[k] != '\0')
{
if (str[k] == 'YOUR LETTER')
count++;
k++;
}
return count;
}
hope this will solve your issues
Cheers:)
Reply:hello friends! i am prabhakar gunturi. iam sure that below solution will work. after that you please send this type of questions to my mail. waiting for your mail.
#include%26lt;stdio.h%26gt;
#include%26lt;conio.h%26gt;
#include%26lt;string.h%26gt;
static int flag[100];
void main()
{
static int count,l,i,j;
char sentence[100];
clrscr();
printf("enter any string");
gets(sentence);
l=strlen(sentence);
for(i=0;i%26lt;l;i++)
{
count=1;
for(j=0;j%26lt;l;j++)
{
if(flag[i]!=1)
{
if(toupper(sentence[i])==toupper(sentenc...
{
count++;
flag[j]=1;
}
}
}
if(flag[i]!=1)
printf("The character %c presents %d times in the given string",sentence[i],count);
}
Reply:If you're looking for a specific character, do:
int CountCharacters(char *str, char find)
{
int count;
for (count=0; *str; str++ )
{
if ( *str == find )
count += 1;
}
return count;
}
Building on that (although definitely not especially efficient since it requires scanning the string for each letter), if you wanted output:
void CountCharacters(char *str)
{
char *find;
char *findPointer;
find = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
for ( findPointer = find; *findPointer; findPointer++ )
{
int count;
count = CountCharacters(str, *findPointer);
if ( count %26gt; 0 )
{
cout %26gt;%26gt; *findPointer %26gt;%26gt; "=" %26gt;%26gt; count %26gt;%26gt; "\r\n";
}
}
}
Reply:qsort(string,sizeof(char),strlen(string)...
char*ptr = string1;
char chars[strlen(string)] = {0,0};
int count[strlen(string)] = {1,1....};
i=0;
while(*ptr)
{
if(*prt == *(ptr+1))
{chars[i] = *ptr;count[i]++;}
else
{chars[i] = *ptr;i++;}
}
survey software
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment