我試圖創建一個簡單的程式來讀取用戶輸入的字串并顯示最長和最短的字串。該程式適用于單個單詞,例如“狗”或“貓”,但如果我輸入“響尾蛇”,它會將其視為兩個單獨的單詞“響尾蛇”和“蛇”,而不是包含空格的整個字串。是否有任何理由將空間注冊為分隔符/分隔符而不將其視為一個完整的字串?謝謝!
#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("s",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;
}
uj5u.com熱心網友回復:
對于初學者來說,這些宣告
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(" [^\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("s",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( " [^\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] );
}
}
程式輸出可能是
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/452789.html
下一篇:對于多行(i 1)的函式?
