从C中的字符串读取元素

发布于 2025-01-18 10:28:43 字数 860 浏览 1 评论 0原文

我试图在用户输入的位置上打印单词的第一个字母。如果有空格,那么它将停止搜索。 有一条有关函数的变暖消息get() 。我的指针和效果有问题,它不会返回字母。我认为孔隙可能在循环时在上。函数firstletter打印单词的第一个字母。例如,如果用户输入先前输入的字符串的索引5,并且在str [5]中对应于“冰淇淋”一词的“ e”,那么firstletter将搜索第一个该词的字母,在这种情况下,它将返回“我”。

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


int firstLetter(int , char *);

int main()
{
    char str[200];
    printf("Insert line: ");
    gets(str);
    
    int pstn;
    scanf("%i,&pstn");
    
    firstLetter(pstn, str);
    
    
    return 0;
}

int firstLetter(int i, char *str)
{
    if(str[i]=' ')
    {
        printf("No letters at this position");
    }
    
    else
    {
        while(str[i]!=' ')
        {
            i--;
        }
        printf("First letter: %c ",str[i+1]);
    }
    return 0;
}

I am trying to print the first letter of a word at the position entered by the user. If there is a whitespace, then it will just stop searching. There is a warming message concerning funtion gets(). I have problems with the pointers and funtion, it just does not return the letter. I think the porblem might be on the while loop. Function firstLetter prints the first letter of a word. For instance, if the user enters index 5 of a previously entered string, and in str[5] corresponds to the 'e' of the word 'ice-cream' then the firstLetter will search for the first letter of that word, in this case it will return 'i'.

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


int firstLetter(int , char *);

int main()
{
    char str[200];
    printf("Insert line: ");
    gets(str);
    
    int pstn;
    scanf("%i,&pstn");
    
    firstLetter(pstn, str);
    
    
    return 0;
}

int firstLetter(int i, char *str)
{
    if(str[i]=' ')
    {
        printf("No letters at this position");
    }
    
    else
    {
        while(str[i]!=' ')
        {
            i--;
        }
        printf("First letter: %c ",str[i+1]);
    }
    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

她说她爱他 2025-01-25 10:28:43

对于这一行的初学者来说,

scanf("%i,&pstn");

你有一个错字。至少你需要重写它,就像

scanf("%i",&pstn);

在函数 firstLetter 中一样,if 语句中也有一个拼写错误

if(str[i]=' ')

,你使用赋值运算符 = 而不是比较运算符 <代码>==。

函数 gets 不安全,不受 C 标准支持。

而是使用函数fgets

fgets( str, sizeof( str ), stdin );

变量pstn 应具有无符号整数类型size_t。否则,用户可以输入负数。

您还需要检查输入的位置是否不大于字符串的长度。

例如,

size_t pstn = 0;
scanf("%zu", &pstn);

if ( pstn < strlen( str ) ) firstLetter(pstn, str);

函数参数应该具有限定符 const,因为传递的字符串在函数内不会更改。

此外,该函数还有一个错误,因为在此 while 循环中

    while(str[i]!=' ')
    {
        i--;
    }

没有检查 i 是否小于 0 。

而且函数的返回类型int 也是无用的。

该函数可以按以下方式声明和定义

void firstLetter( const char *str, size_t i )
{
    if ( str[i] == ' ' )
    {
        printf("No letters at this position");
    }
    else
    {
        while( i != 0 && str[i-1] != ' ' )
        {
            i--;
        }

        printf("First letter: %c\n", str[i] );
    }
}

所以在 main 中调用该函数

size_t pstn = 0;
scanf("%zu", &pstn);

if ( pstn < strlen( str ) ) firstLetter( str, pstn );

,或者您可以检查字符串中的指定位置是否小于函数内字符串的长度,例如

void firstLetter( const char *str, size_t i )
{
    if ( !( i < strlen( str ) ) || str[i] == ' ' )
    {
        printf("No letters at this position");
    }
    else
    {
        while( i != 0 && str[i-1] != ' ' )
        {
            i--;
        }

        printf("First letter: %c\n", str[i] );
    }
}

在这种情况下该函数被称为像

size_t pstn = 0;
scanf("%zu", &pstn);

firstLetter( str, pstn );

而不是与空格字符 ' ' 进行比较,您可以与空格字符 ' ' 和制表符 '\t' 进行比较> 使用标准 C 函数 isblank 声明于标头。例如

#include <string.h>
#include <ctype.h>

void firstLetter( const char *str, size_t i )
{
    if ( !( i < strlen( str ) ) || isblank( ( unsigned char )str[i] ) )
    {
        printf("No letters at this position");
    }
    else
    {
        while( i != 0 && !isblank( ( unsigned char )str[i-1] ) )
        {
            i--;
        }

        printf("First letter: %c\n", str[i] );
    }
}

For starters in this line

scanf("%i,&pstn");

you have a typo. At least you need to rewrite it like

scanf("%i",&pstn);

Also within the function firstLetter there is also a typo in the if statement

if(str[i]=' ')

where you are using the assignment operator = instead of the comparison operator ==.

The function gets is unsafe and is not supported by the C Standard.

Instead use the function fgets

fgets( str, sizeof( str ), stdin );

The variable pstn should have the unsigned integer type size_t. Otherwise the user can enter a negative number.

Also you need to check that the entered position is not greater than the length of the string.

For example

size_t pstn = 0;
scanf("%zu", &pstn);

if ( pstn < strlen( str ) ) firstLetter(pstn, str);

The function parameter should have the qualifier const because the passed string is not changed within the function

Also the function has a bug because in this while loop

    while(str[i]!=' ')
    {
        i--;
    }

there is no check whether i is less than 0.

Also the return type int of the function is useless.

The function can be declared and defined the following way

void firstLetter( const char *str, size_t i )
{
    if ( str[i] == ' ' )
    {
        printf("No letters at this position");
    }
    else
    {
        while( i != 0 && str[i-1] != ' ' )
        {
            i--;
        }

        printf("First letter: %c\n", str[i] );
    }
}

So in main the function is called like

size_t pstn = 0;
scanf("%zu", &pstn);

if ( pstn < strlen( str ) ) firstLetter( str, pstn );

Or you can make the check that the specified position in the string is less than the length of the string within the function as for example

void firstLetter( const char *str, size_t i )
{
    if ( !( i < strlen( str ) ) || str[i] == ' ' )
    {
        printf("No letters at this position");
    }
    else
    {
        while( i != 0 && str[i-1] != ' ' )
        {
            i--;
        }

        printf("First letter: %c\n", str[i] );
    }
}

In this case the function is called like

size_t pstn = 0;
scanf("%zu", &pstn);

firstLetter( str, pstn );

Instead of the comparison with the space character ' ' you could compare with the space character ' ' and the tab character '\t' using the standard C function isblank declared in the header <ctype.h>. For example

#include <string.h>
#include <ctype.h>

void firstLetter( const char *str, size_t i )
{
    if ( !( i < strlen( str ) ) || isblank( ( unsigned char )str[i] ) )
    {
        printf("No letters at this position");
    }
    else
    {
        while( i != 0 && !isblank( ( unsigned char )str[i-1] ) )
        {
            i--;
        }

        printf("First letter: %c\n", str[i] );
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文