Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.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语言中,如何仅strcmp开头的2个字符,然后连接?_C_String_String Comparison_Strcmp_Strncmp - Fatal编程技术网

在C语言中,如何仅strcmp开头的2个字符,然后连接?

在C语言中,如何仅strcmp开头的2个字符,然后连接?,c,string,string-comparison,strcmp,strncmp,C,String,String Comparison,Strcmp,Strncmp,在C语言中,如何仅使用开头的2个字符?然后用另一个字符串连接?大概是这样的: char s[10]; scanf("%s",s); /* if i input "cs332" or "cs234", anything start with cs */ if (strcmp("cs",???)==0) strcat(s,"by professor"); char str[80]; snprintf(str, 80, "%s by professor", s); 使用您可以使用的st

在C语言中,如何仅使用开头的2个字符?然后用另一个字符串连接?大概是这样的:

char s[10];
scanf("%s",s);

/* if i input "cs332" or "cs234", anything start with cs */

if (strcmp("cs",???)==0)
    strcat(s,"by professor");
char str[80];
snprintf(str, 80, "%s by professor", s);
使用您可以使用的strncmp

编辑:

strcat(s,"by professor");

// s is an array of 10 characters. You need to make sure s is big enough  
// to hold the string that needs to be concatenated + to have a terminating 
// character '\0'.

为什么不直接比较字符而不是调用strcmp

例如

如果[0]=='c'和&s[1]=='s'{ ...
}有几种方法可以做到这一点

字符串比较:

这是一种天真的方式,因为您无法轻松地将其扩展到稍长的代码,例如长度为3/4个字符

我想你已经意识到你应该使用它,对吗

串接

不要使用strcat。真正地如果将两个长度大于目标大小的字符串连接在一起,则会遇到麻烦。考虑使用,像这样:

char s[10];
scanf("%s",s);

/* if i input "cs332" or "cs234", anything start with cs */

if (strcmp("cs",???)==0)
    strcat(s,"by professor");
char str[80];
snprintf(str, 80, "%s by professor", s);
或者,正如希思所指出的那样:

char s[80];
strncat(s, " by professor", 80);
您正在寻找strncmp函数,该函数在功能上与strcmp相同,但限制检查的字符数。因此,您可以将其与长度为2和比较字符串cs一起使用。但是,这里还有一些其他问题

首先,缓冲区不够大。当您将professor提供的文本附加到10个字符的缓冲区中时,没有任何字符串适合该缓冲区

其次,健壮的代码永远不会将scanf与无界字符串格式说明符一起使用:这会导致缓冲区溢出问题。scanf系列适用于格式化输入,与用户输入相比,无格式输入几乎没有什么区别:-

如果需要强健的输入解决方案,请参阅

第三,您应该始终假设连接字符串可能会使缓冲区溢出,并引入代码来防止这种情况。你需要加起来:

用户输入的字符串的当前长度。 教授指定的附加字符串的长度。 空终止符还有一个。 并确保缓冲区足够大

我将使用的方法是使用一个例如200字节的缓冲区,使用下面复制的链接答案中的getLine使这个答案以足够小的大小(比如100)自包含,然后您可以放心,professor的追加不会使缓冲区溢出

功能:

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

#define OK       0
#define NO_INPUT 1
#define TOO_LONG 2
static int getLine (char *prmpt, char *buff, size_t sz) {
    int ch, extra;

    // Get line with buffer overrun protection.
    if (prmpt != NULL) {
        printf ("%s", prmpt);
        fflush (stdout);
    }
    if (fgets (buff, sz, stdin) == NULL)
        return NO_INPUT;

    // If it was too long, there'll be no newline. In that case, we flush
    // to end of line so that excess doesn't affect the next call.
    if (buff[strlen(buff)-1] != '\n') {
        extra = 0;
        while (((ch = getchar()) != '\n') && (ch != EOF))
            extra = 1;
        return (extra == 1) ? TOO_LONG : OK;
    }

    // Otherwise remove newline and give string back to caller.
    buff[strlen(buff)-1] = '\0';
    return OK;
}

是的,如前所述,strcmp是首选方法。这里有一个不同的方法来做同样的事情

#define CS 29539
char s[80];

scanf("%60s", s);
if( *(short *)s == CS )
    if( strlcat(s, " by professor", sizeof(s)) >= sizeof(s) )
        fprintf(stderr, "WARNING: truncation detected: %s", s);

为什么不是strncat?“比snprintf快多了。”希思·费尔·波因特说,它会很好用的。我想我刚开始接触snprintf。大约一年前,我写了自己的snprintf。这比我想象/害怕的要容易得多。这也是教育性的。由于格式字符串解析,结果没有我在编写之前想象的那么慢。我也只支持ASCII/ANSI,类似于K&R sprintf+n,这使得它成为snprintf。我只是想和大家分享一下,因为我鼓励人们写一个snprintf来获得极大的乐趣。不!孩子们,请不要在家里这样做。
#define CS 29539
char s[80];

scanf("%60s", s);
if( *(short *)s == CS )
    if( strlcat(s, " by professor", sizeof(s)) >= sizeof(s) )
        fprintf(stderr, "WARNING: truncation detected: %s", s);