Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何检查字符串C中的空格?_C_Arrays_Pointers_Error Handling - Fatal编程技术网

如何检查字符串C中的空格?

如何检查字符串C中的空格?,c,arrays,pointers,error-handling,C,Arrays,Pointers,Error Handling,我试图让我的程序从用户那里获取一个名称作为输入,然后打印出该名称的首字母。i、 e.汤米·布朗 到目前为止,我得到的是: int main (void) { char scroll[100] = {"kang cheng junga"}; printf ("%c\n", *(scroll+2)); for (int i=0; i<100; i++) { if (*(scroll+i)=" ") { printf (*(scroll+i+1));

我试图让我的程序从用户那里获取一个名称作为输入,然后打印出该名称的首字母。i、 e.汤米·布朗

到目前为止,我得到的是:

int main (void)
{

char scroll[100] = {"kang cheng junga"};
printf ("%c\n", *(scroll+2));

for (int i=0; i<100; i++)
{
    if (*(scroll+i)=" ")
    {
        printf (*(scroll+i+1));
    }
}
int main(无效)
{
char scroll[100]={“kang cheng junga”};
printf(“%c\n”,*(滚动+2));

对于C中的(int i=0;i,
=
是赋值运算符,
=
是相等运算符。您需要使用后面的运算符


这就是说,有一个很好的库函数,名为,它可以让您的生活更轻松。只需传递字符串和分隔符,(这里是空格
)它会将令牌返回给您,您可以打印第一个字符以获得首字母。

与strtok或strchr一起,这里是一种简单的实现方法,缺少一些边界条件,例如名称以单字母结尾等

#include <stdio.h>

int main (void)
{

        int i = 0;
        char scroll[100] = {"kang cheng junga"};
        char *p = scroll; // point to base
        printf("%c", *p ); // first letter is almost always certain
        while( *p != '\0' ) { // till end of String

                if ( *(p-1)     == ' ' && *(p+1) != '\0') { // If previous is space and next letter exists
                        printf("%c", *p );      // take it save it or print it
                }
                p ++ ; //proceed
        }

        return 0;
}
#包括
内部主(空)
{
int i=0;
char scroll[100]={“kang cheng junga”};
char*p=scroll;//指向底部
printf(“%c”,*p);//第一个字母几乎总是确定的
while(*p!='\0'){//
如果(*(p-1)=''&*(p+1)!='\0'){//如果上一个是空格,下一个字母存在
printf(“%c”,*p);//获取它保存它或打印它
}
p++;//继续
}
返回0;
}

您得到的错误是由于赋值运算符(=)引起的


除了
strtok
,您还可以利用
strpbrk
轻松找到每个
空间

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

int main (void) {

    char scroll[100] = "kang cheng junga";
    char *p = scroll;

    printf ("\n full name: %s\n", scroll);
    printf (" initials : %c", *p);

    while ((p = strpbrk (p, " ")))
            printf ("%c", *++p);

    printf ("\n\n");

    return 0;
}
您还可以使用单独使用指针的替代版本来消除对
string.h
的依赖:

#include <stdio.h>

int main (void) {

    char scroll[100] = "kang cheng junga";
    char *p = scroll;

    printf ("\n full name: %s\n", scroll);
    printf (" initials : %c", *p);

    while (*p++)
        if (*p == ' ' && *++p)
            printf ("%c", *p);

    printf ("\n\n");

    return 0;
}
#包括
内部主(空){
字符卷轴[100]=“康成容加”;
char*p=滚动;
printf(“\n全名:%s\n”,滚动);
printf(“姓名首字母:%c”,*p);
而(*p++)
如果(*p==''&*++p)
printf(“%c”,*p);
printf(“\n\n”);
返回0;
}

基本内容:
=
应该是
=
如果(*(滚动+i)=“”)如果(*(滚动+i)=“”)比较字符而不是字符和字符串。@noggy还有为什么将限制设置为100?。不必要地执行for循环100次*(滚动+i)=“”;->*(滚动+i)=='';此方法引发警告:
指针和整数之间的比较
,我不允许我的代码有任何警告,这是一种不好的做法。我很好奇在检测到第一个空格后如何打印字符串的其余部分,如“cheng junga”,在这种情况下,简单的方法是
p=strhr(滚动),;put(p+1)
这工作得很好。现在,只要我试图通过输入流“for”(I=0;I)读取“scroll”,当您开始从输入流读取时,您应该使用
fgets()
将一行一行读取到缓冲区中,然后使用
sscanf()从缓冲区解析您需要的内容
。这样可以确保每次读取时使用一行输入,并且格式的变化不会打断您的输入。例如
char buf[256];while(fgets(buf,sizeof buf,stdin)){char first[64],middle[64],last[64];if(sscanf(buf,%s%s%s%s),first,middle,last)==3)printf(\n first:%s\n中间:%s\n last:%s\n,first,middle,last)}
您可以根据需要复制
buf
或任何子字符串进行保存。
$ ./bin/initials

 full name: kang cheng junga
 initials : kcj
#include <stdio.h>

int main (void) {

    char scroll[100] = "kang cheng junga";
    char *p = scroll;

    printf ("\n full name: %s\n", scroll);
    printf (" initials : %c", *p);

    while (*p++)
        if (*p == ' ' && *++p)
            printf ("%c", *p);

    printf ("\n\n");

    return 0;
}