簡體   English   中英

計算字符串長度時包括空格

[英]Include spaces when counting the length of a string

我試圖創建一個簡單的程序來讀取用戶輸入的字符串並顯示最長和最短的字符串。 該程序適用於單個單詞,例如“dog”或“cat”,但如果我輸入“rattle snake”,它會將其視為兩個單獨的單詞“rattle”和“snake”,而不是一個包含空格的完整字符串。 是否有任何理由將空間注冊為分隔符/分隔符而不將其視為一個完整的字符串? 謝謝!

#include<stdio.h>
#include<string.h>

int main()
{
//DEFINE VALUES N & I COUNTERS
//DEFINE VALUES FOR MAX AND MIN INDEX POINTERS
//DEFINE WORD LENGTH ARRAY
//DEFINE MAX AND MIN LENGTH VALUE VARIABLES USING STRLEN FUNCTION
int n,i;
size_t maxIndex=0;
size_t minIndex=0;
char word_length[2][20];
size_t maxLength =strlen(word_length[0]);
size_t minLength =strlen(word_length[0]);
    /*USER ENTERS LIST LENGTH*/
    printf("How many items are in your list?\n");
    scanf("%d",&n);
    
    /*USER ENTERS THE WORDS ONE AFTER ANOTHER USING THE ENTER BUTTON*/
    printf("Please enter a list of words \n ---------------------\n");
    

    /*SEARCH FROM INDEX POINTER 0-LENGTH OF LIST*/
    for (size_t i=0;i<n;i++)
    {
        /*CALCULATE THE LENGTH OF EACH WORD AND STORE IT IN WORD LENGTH ARRAY*/
        scanf("%19s",word_length[i]);
        /*NEW VARIABLE LENGTH IS UPDATED TO LENGTH OF EACH STRING HELD IN EACH POINTER IN WORD_LENGTH ARRAY*/
        size_t length = strlen(word_length[i]);
        /*IF THE NEW LENGTH IS BIGGER THAN THE PREVIOUS MAXIMUM LENGTH, THE MAXIMUM LENGTH SCORE IS UPDATED*/
        /*THE MAXINDEX VARIABLE KEEPS A RECORD OF WHICH ARRAY POSITION THIS STRING IS HELD IN*/
        /*THE SAME HAPPENS FOR THE MIN LENGTH ARRAY*/
        if (maxLength<length) {
            maxLength = length;
            maxIndex = i;
        }
        else if (length<minLength){
            minLength=length;
            minIndex=i;
        }
    }
    

    /*THE BIGGEST WORD IS DISPLAYED ON SCREEN USING THE MAXINDEX POINTER*/
    /*THE SMALLEST WORD IS DISPLAYED ON SCREEN USING THE MAXINDEX POINTER*/
    printf("\nThe biggest word is '%s' with %zu characters\n",word_length[maxIndex],strlen(word_length[maxIndex]));
    printf("'%s' is stored in position %zu \n",word_length[maxIndex],maxIndex);
    printf("\nThe smallest word is '%s' with %zu characters\n",word_length[minIndex],strlen(word_length[minIndex]));
    printf("'%s' is stored in position %zu \n",word_length[minIndex],minIndex);

return 0;
}

對於初學者這些聲明

size_t maxLength =strlen(word_length[0]);
size_t minLength =strlen(word_length[0]);

調用未定義的行為,因為數組word_length未初始化

char word_length[2][20];

你需要初始化它,例如

char word_length[2][20] = { '\0' };

要讀取帶有嵌入空格的字符串,您可以使用以下 scanf 調用

scanf(" %19[^\n]",word_length[i]);

還要注意你聲明了一個包含兩個元素的數組

char word_length[2][20];

但是您正在使用具有 n 次迭代的 for 循環

for (size_t i=0;i<n;i++)
{
    /*CALCULATE THE LENGTH OF EACH WORD AND STORE IT IN WORD LENGTH ARRAY*/
    scanf("%19s",word_length[i]);
    //...

這又可以調用未定義的行為。

比如你需要再引入一個字符數組

char word[20];

並在此數組的 for 循環中讀取字符串。

該程序可以通過以下方式查找示例

#include <stdio.h>
#include <string.h>

int main( void )
{
    enum { M = 2, N = 20 };
    enum { MIN = 0, MAX = 1 };

    char min_max_word[M][N];
    size_t min_max_length[M] = { 0, 0 };
    size_t min_max_index[M] = { 0, 0 };

    size_t n = 0;

    printf( "How many items are in your list? " );
    scanf( "%zu", &n );

    size_t i = 0;

    for ( char word[N]; i < n && scanf( " %19[^\n]", word ) == 1; i++ )
    {
        size_t length = strlen( word );

        if ( i == 0 )
        {
            strcpy( min_max_word[MIN], word );
            min_max_length[MIN] = length;
            min_max_index[MIN] = i;

            strcpy( min_max_word[MAX], word );
            min_max_length[MAX] = length;
            min_max_index[MAX] = i;
        }
        else if ( length < min_max_length[MIN] )
        {
            strcpy( min_max_word[MIN], word );
            min_max_length[MIN] = length;
            min_max_index[MIN] = i;
        }
        else if ( min_max_length[MAX] < length )
        {
            strcpy( min_max_word[MAX], word );
            min_max_length[MAX] = length;
            min_max_index[MAX] = i;
        }
    }

    if (i != 0)
    {
        printf( "\nThe biggest word is '%s' with %zu characters\n", min_max_word[MAX],  min_max_length[MAX] );
        printf( "'%s' is stored in position %zu \n", min_max_word[MAX], min_max_index[MAX] );
        printf( "\nThe smallest word is '%s' with %zu characters\n", min_max_word[MIN], min_max_length[MIN] );
        printf( "'%s' is stored in position %zu \n", min_max_word[MIN], min_max_index[MIN] );
    }
}

程序 output 可能是

How many items are in your list? 5
1
12
123
1234
12345

The biggest word is '12345' with 5 characters
'12345' is stored in position 4

The smallest word is '1' with 1 characters
'1' is stored in position 0

暫無
暫無

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

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