从C中的字符串读取元素
我试图在用户输入的位置上打印单词的第一个字母。如果有空格,那么它将停止搜索。 有一条有关函数的变暖消息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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于这一行的初学者来说,
你有一个错字。至少你需要重写它,就像
在函数
firstLetter
中一样,if 语句中也有一个拼写错误,你使用赋值运算符
=
而不是比较运算符 <代码>==。函数
gets
不安全,不受 C 标准支持。而是使用函数
fgets
变量
pstn
应具有无符号整数类型size_t
。否则,用户可以输入负数。您还需要检查输入的位置是否不大于字符串的长度。
例如,
函数参数应该具有限定符 const,因为传递的字符串在函数内不会更改。
此外,该函数还有一个错误,因为在此 while 循环中
没有检查
i
是否小于0 。
而且函数的返回类型
int
也是无用的。该函数可以按以下方式声明和定义
所以在 main 中调用该函数
,或者您可以检查字符串中的指定位置是否小于函数内字符串的长度,例如
在这种情况下该函数被称为像
而不是与空格字符
' '
进行比较,您可以与空格字符' '
和制表符'\t'
进行比较> 使用标准 C 函数isblank
声明于标头
。例如For starters in this line
you have a typo. At least you need to rewrite it like
Also within the function
firstLetter
there is also a typo in the if statementwhere 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
The variable
pstn
should have the unsigned integer typesize_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
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
there is no check whether
i
is less than0
.Also the return type
int
of the function is useless.The function can be declared and defined the following way
So in main the function is called like
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
In this case the function is called like
Instead of the comparison with the space character
' '
you could compare with the space character' '
and the tab character'\t'
using the standard C functionisblank
declared in the header<ctype.h>
. For example