在任务的这一点上卡住了。有一个60个字符的块(字符),我的任务是必须打印出该块中单词的最后字母。我的建议是在(空格)“”之前应打印出居留信。我的意思是一个功能检查所有字母,当有空格时,它应该算回一个字符并打印出来。 从现在开始,我真的找不到用于检查所有字符的正确功能。 Isalpha函数可以帮助我吗?
答案 0 :(得分:-1)
在这里。我认为该字符串仅包含字母和空格。这是一个由空格限制的字符序列。
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *s = "How to print out the last letters of the words in C";
const char * blanks = " \t";
for ( size_t i = 0; s[i] != '\0'; )
{
size_t j = strspn( s + i, blanks );
i += j;
j = strcspn( s + i, blanks );
i += j;
if ( j != 0 ) printf( "%c ", s[i-1] );
}
return 0;
}
程序输出为
w o t t e t s f e s n C
如果您需要以大写字母输出字母,请添加标题<ctype.h>
并更改输出语句,例如
if ( j != 0 ) printf( "%c ", toupper( ( unsigned char )s[i-1] ) );