0

I found this example

int SizeofCharArray(char *phrase)
{
  int size = 0;

  int value = phrase[size];

  while(value != 0)
  {
       value = phrase[size];
       size++;

  };

  //printf("%i%s", size, "\n");

  return size;
}

here

But how can I count number of letters in string array using pure C? Even I do not understand how can I initialize string array?!

Thank you!

4
  • 1
    Do you literally mean letters, as in "ABC" but not "123", or characters? Commented Jul 10, 2015 at 22:17
  • int size = strlen(phrase) gets the size. You need to initialize phrase either on the stack in the calling function, or use malloc/free to allocate memory. Commented Jul 10, 2015 at 22:17
  • 1
    Read stackoverflow.com/tags/c/info and the book section stackoverflow.com/a/562377/13422 Commented Jul 10, 2015 at 22:19
  • This question seem to be two seperate questions. Commented Jul 11, 2015 at 7:32

4 Answers 4

1

The posted code is of rather poor quality. The name of the function, SizeofCharArray, does not match the description, count number of letters in string array.

If you want to return the number of characters in the array, use:

int SizeofCharArray(char *phrase)
{
   int size = 0;
   char* cp = phrase;

   while( *cp != '\0')
   {
      size++;
      cp++;
   };

   return size;
}

If you want to return the number of letters in the array, use:

int isLetter(char c)
{
   return (( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ));
}

int GetNumberOfLetters(char *phrase)
{
   int num = 0;
   char* cp = phrase;

   while( *cp != '\0')
   {
      if ( isLetter(*cp) )
      {
         num++;
      }
      cp++;
   };

   return num;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I should have '\0' where it has '0'.
All ints should be size_ts. At least there is no ned for nehative values here.
0

This will count the number of alphabetic characters in a c-string:

#include <ctype.h>

int numberOfLetters(char *s)
{
    int n = 0;
    while (*s)
        if (isalpha(*s++))
            n++;
    return n;
}

If you want the actual number of characters, counting characters like spaces and numbers, just use strlen(s) located in string.h.

Comments

0

To find the length of C string, you can use strlen() function

    #include<string.h>

    char str[]="GJHKL";
    const char *str1="hhkjj";
    int len1=strlen(str)<<"\n";
    int len2=strlen(str1);

Comments

0

It's not particularly good C. I doesn't give you the size of a char array -- that's impossible to determine if you've lost that information. What it does give you is the size of a null-terminated char array (AKA a c-string), and it does so by counting the characters until it finds the null-terminator (0 byte or '\n'). As a matter of fact, what you've got up top is a not particularly good strlen implementation (strlen is a standard library function that does the same thing -- determine the size of a null-terminated char array)

I believe this below should be a little more C-ish implementation of the same thing:

size_t strlen(const char *s){ 
  const char* ptr = s;
  for(; *ptr; ++ptr); //move the pointer until you get '\0'
  return ptr-s; //return the difference from the original position (=string length;)
}

It returns size_t (64 bit unsigned int if you're on a 64 bit machine and 32 on 32 machines, so it will work on arbitrarily long strings as long as they fit into memory) and it also declares that it won't modify the array it measures (const char *s means a pointer you promise not to use to change what it points to).

1 Comment

Terrible bug: This will likely crash when you pass an empty string. I think using unnecessarily complicated expressions is a direct reason for the bug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.