简体   繁体   English

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

[英]How to check spaces in a string C?

I am trying to have my program take a name as input from the user and then print out the initials of that name.我试图让我的程序从用户那里输入一个名称,然后打印出该名称的首字母。 ie tommy brown --> tb即汤米布朗 --> tb

What I have so far is this:到目前为止,我所拥有的是:

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 keep getting this error:我不断收到此错误:

error: incompatible pointer to integer conversion assigning to 'char' from 'char [2]'错误:从“char [2]”分配给“char”的整数转换指针不兼容

[-Werror,-Wint-conversion] if (*(scroll+i)=" ") [-Werror,-Wint-conversion] if (*(scroll+i)="")

error: using the result of an assignment as a condition without parentheses错误:使用赋值结果作为不带括号的条件

[-Werror,-Wparentheses] if (*(scroll+i)=" ") [-Werror,-W括号] if (*(scroll+i)="")

Can anyone tell me how I've screwed this up?谁能告诉我我是怎么搞砸的? I am having a hard time understanding how * and & function in a C. I am a beginner so I don't really know what I'm doing.我很难理解 * 和 & 在 C 中的功能。我是一个初学者,所以我真的不知道我在做什么。

In addition to strtok , this is one time you can also make use of strpbrk to easily find each space :除了strtok ,这是您还可以利用strpbrk轻松找到每个space

#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;
}

Output输出

$ ./bin/initials

 full name: kang cheng junga
 initials : kcj

You can also eliminate the dependence on string.h with an alternative version that uses pointers alone:您还可以使用单独使用指针的替代版本来消除对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;
}

The Error you are getting is because of the assignment operator (=) .你得到的错误是因为赋值运算符 (=) 。

if (*(scroll+i)=" ")// assigning a value(blank)
{
    printf (*(scroll+i+1));
}

if (*(scroll+i) == ' ')//Comparing it with a value (blank)
{
    printf (*(scroll+i+1));
}

In C, = is assignment operator, and == is equality operator.在 C 中, =是赋值运算符,而==是相等运算符。 You need to use the later one.您需要使用后者。

That said, there is a nice library function, named strtok() which can make your life easier.也就是说,有一个很好的库函数,名为strtok() ,它可以让您的生活更轻松。 Just pass the string and a delimiter, (here, the space只需传递字符串和分隔符,(这里是空格) and it will return the token to you, for which you can print the first character to get the initials. ),它会将令牌返回给您,您可以打印第一个字符以获取首字母。

along with strtok or strchr here is a simple way to implement , some boundary conditions are missing like name ending with single letter etc与 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM