计算字符串长度时包括空格
Include spaces when counting the length of a string
我正在尝试创建一个简单的程序来读取用户输入的字符串并显示最长和最短的字符串。该程序适用于 'dog' 或 'cat' 等单个单词,但如果我输入 'rattle snake',它会将其视为两个单独的单词 'rattle' 和 'snake' 以及没有包含 space 的整个字符串。为什么将 space 注册为 divider/separator 而不是将其视为一个完整的字符串?谢谢!
#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] = { '[=12=]' };
要读取带有嵌入空格的字符串,您可以使用以下 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] );
}
}
程序输出可能是
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
我正在尝试创建一个简单的程序来读取用户输入的字符串并显示最长和最短的字符串。该程序适用于 'dog' 或 'cat' 等单个单词,但如果我输入 'rattle snake',它会将其视为两个单独的单词 'rattle' 和 'snake' 以及没有包含 space 的整个字符串。为什么将 space 注册为 divider/separator 而不是将其视为一个完整的字符串?谢谢!
#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] = { '[=12=]' };
要读取带有嵌入空格的字符串,您可以使用以下 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] );
}
}
程序输出可能是
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