簡體   English   中英

計算字符串數組中的字符?

[英]Count characters in an array of character strings?

給定一個字符串數組,例如...

char *example[] = {"s", "ss", "sss"};

如何在不使用標准庫的strlen()等函數的情況下編寫函數以計算數組中包括終止字符的字符總數。

跟隨是我的嘗試

int countChars(char *array[], int len)
{
    int total = 0, count = 0;


    for (int i = 0; i < len; i++)
    {
        if (array[i] != NULL)
        {
            while (*array[i] != '\0') {
                count++;
            }
            count++;
        }
        total += count;
    }

    return total;
}

有關char * array []如何實際用於訪問wold的解釋,不勝感激。 我認為它應該是指向字符串的指針數組。

  • 您必須增加索引才能考慮每個字符。

像這樣的東西:

for (int i = 0; i < len; i++)
{
    if (array[i] != NULL)
    {
        int j=0,count=0;
        while (array[i][j++] != '\0') {
            count++;
        }
        total += count;     
    }

}
  • 在所有計算結束時,還可以重置count或將total相加。

作為對第二個問題的回答:


char* array[]基本上表示一個數組指針,每個指針都指向用來初始化它的字符串文字。

因此,一旦使用array[i]您現在應該認為它只是指向字符串文字的指針。

您需要為每個處理過的字符串在for循環內重新初始化變量count ,並在while循環內增加表達式*array[i]

函數具有返回類型size_tsize_t是標准C函數strlen和運算符sizeof返回的類型)也更好。

該函數可以看起來像演示程序中顯示的那樣。

#include <stdio.h>

size_t countChars( const char *array[], size_t n )
{
    size_t count = 0;

    while ( n-- )
    {
        if ( array[n] )
        {
            size_t i = 0;
            do { ++count; } while ( array[n][i++] );
        }
    }

    return count;
}

int main(void) 
{
    const char * example[] = { "s", "ss", "sss" };

    printf( "%zu\n", countChars( example, sizeof( example ) / sizeof( *example ) ) );

    return 0;
}

程序輸出為

9

該數組的每個元素

char *example[] = {"s", "ss", "sss"};

具有char *類型,並且是指向相應字符串文字的第一個字符的指針。

由於數組包含字符串常量,因此應使用const聲明:

const char *example[3];

如果沒有const ,則嘗試將字符分配給示例 [ i ] [ j ]時,編譯器不會警告您。 出於同樣的原因,形式參數也應該用const聲明。

對於沒有副作用的純函數,最好對其進行命名,以使其反映結果。 為此我會用charCount代替countChars(或者totalLength)。 重點應放在名詞 (即countlength )上。

這是我的解決方案:

#include <stdio.h>

#define LEN(a) (sizeof (a) / sizeof (a)[0])

static int CharCount(const char *strings[], int len)
{
    int result, i, j;

    result = 0;
    for (i = 0; i < len; i++) {
        j = 0;
        while (strings[i][j] != '\0') {
            result++;
            j++;
        }
    }
    return result;
}


int main(void)
{
    const char *strings[] = { "s", "ss", "sss" };

    printf("character count: %d\n", CharCount(strings, LEN(strings)));
}

長度宏LEN非常方便,並且是處理數組長度時最不容易出錯的方法。

char *array[] = {"aa", "bb", "cc"}是指向字符串的指針數組。

  • array[0]指向"aa"
  • array[1]指向"bb"
  • array[2]指向"cc"

您可能想要這樣:

int countChars(char *array[], int len)
{
  int count = 0;

  for (int arrayindex = 0; arrayindex < len; arrayindex++)
  {
    const char *stringptr = array[arrayindex];
                                // stringptr will point successively
                                // to "s", to "ss" and to "sss"

    while (*stringptr++)
      count++;                  // increment count until NUL character encountered
    count++;                    // one more for NUL character
  }

  return count;
}

int main() {
  char *example[] = { "s", "ss", "sss" };

  int x = countChars(example, 3);   // x contains 9 after the call to countChars
                                    // that is 2 + 3 + 4
}

可以使用sizeof(example) / sizeof(example[0])代替硬編碼3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM