小编use*_*325的帖子

Strncmp 实现

我有以下带有测试驱动程序的 strncmp 函数实现,但是无法编译。

我也不确定逻辑是否正确。这是来自我的编译器的错误消息:

警告:控件可能会到达非空函数的结尾 [-Wreturn-type]

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

#undef strncmp

int strncmp(const char *s, const char *t, size_t num)
{
    for ( ; num >0;  s++, t++, num--)
        if (*s == 0)
            return 0;

    if (*s == *t) {
        ++s;
        ++t;
    }
    else if (*s != *t)
        return *s - *t;  
}

 int main ()
 {
   char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
   int n;
   puts ("Looking for R2 astromech droids...");
   for (n=0 ; n<3 …
Run Code Online (Sandbox Code Playgroud)

c string compare

2
推荐指数
1
解决办法
1万
查看次数

KR练习4.7 Ungets功能测试

我需要编写一个函数ungets(s),将整个字符串推回输入.我不知道ungets的实现是否正确.我不知道如何测试它,任何帮助将不胜感激.

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

/* Implementation */    

#define BUFSIZE 100
static char buf[BUFSIZE];
static int bufp = 0;             /* next free position in buf */

int getch(void)           /* get a (possibly pushed back) character */
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}


void ungetch(int c)   /* push character back on input  */
{
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}


void ungets(char *s)  
{
    int c;

    for (c = 0; …
Run Code Online (Sandbox Code Playgroud)

c string kernighan-and-ritchie

0
推荐指数
1
解决办法
274
查看次数

标签 统计

c ×2

string ×2

compare ×1

kernighan-and-ritchie ×1