Victor Graff
Victor Graff

Reputation:

Initialize string array in C and count number of letters inside

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!

Upvotes: 0

Views: 848

Answers (4)

Petr Skocik
Petr Skocik

Reputation: 60097

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).

Upvotes: 0

R Sahu
R Sahu

Reputation: 206667

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;
}

Upvotes: 1

Steephen
Steephen

Reputation: 15824

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);

Upvotes: 0

Alyssa Haroldsen
Alyssa Haroldsen

Reputation: 3731

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.

Upvotes: 0

Related Questions