Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include<stdio.h>
#include<string.h>
void frequency_ptr(char *str);
int main()
{
    char strings[30];
    printf("Enter a string:");
    gets(strings);
    frequency_ptr(&strings[0]);
}
void frequency_ptr(char *strings)
{
    int i=0;
    static int cnt[26];
    while(*strings[i]!='\0')
    {
    if(*strings[i]>='a' && *strings[i]<='z')
    cnt[*strings[i]-'a']++;   
    i++;
    }
    for(i=0;i<26;i++)
        printf("\n%c occurs %d times:", 'a'+i, cnt[i]);
}


What I have tried:

The below program works fine for me. But why am i getting error in the above while loop.
C++
while(strings[i]!='\0')
 {
    if(strings[i]>='a' && strings[i]<='z')
    cnt[strings[i]-'a']++;
    i++;
 }


Thanks in advance.
Posted
Updated 31-Jul-18 23:15pm
v4
Comments
Patrice T 31-Jul-18 17:35pm    
'But why am i getting error in the above while loop.'
What error message ? and where ?

The code at "What I have tried" is correct.

It is a common C/C++ beginners problem to understand the relation of pointers and arrays.

But it is covered by any C/C++ books and tutorials (search the web for something like "array vs pointer").

See for example the section "Pointers and Arrays" at C Class - Arrays, String Constants and Pointers[^].

In your case you have the function parameter char *strings. That means strings is a pointer to char values. Using the Dereference operator - Wikipedia[^], you would access the first character of the string:
C++
char first_char = *strings;
That can be also done with the array access [] operator:
C++
char first_char = strings[0];

So using
C++
char c = *strings[0];
is the same as
C++
char c = **strings;
and both result in a compilation error because the above would be only valid when strings is of type char ** (pointer to pointer to char).
 
Share this answer
 
Comments
Member 13922884 1-Aug-18 5:09am    
Thank you !!
Jochen Arndt 1-Aug-18 5:33am    
You are welcome and thank you for accepting my solution.
Don't ever use gets, use fgets instead. Try:
C
#include<stdio.h>
void frequency_ptr(const char *str);

#define SIZE 30
#define LETTERS 26

int main()
{
  char string[SIZE];
  printf("Enter a string:");
  fgets(string, sizeof(string), stdin);
  frequency_ptr(string);
  return 0;
}

void frequency_ptr(const char * str)
{
  unsigned int cnt[LETTERS]={0};
  while(*str != '\0')
  {
    if(*str>='a' && *str<='z')
      cnt[*str-'a']++;
    ++str;
  }
  size_t i;
  for(i=0; i<LETTERS;i++)
    printf("%c occurs %d times.\n", (char)('a'+i), cnt[i]);
}
 
Share this answer
 
Comments
Member 13922884 1-Aug-18 5:09am    
Thank you !!
CPallini 1-Aug-18 5:14am    
You are welcome.
KarstenK 1-Aug-18 5:19am    
By removing the i you changed the code more than needed. (Bad for comparisons) And I dont like changing input paramters. It is always confusing. ;-)
CPallini 1-Aug-18 5:59am    
That is a common idiom with C-strings.
What you (and I) like doens't much matter. :-)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900