size_t strtok

本文介绍了C语言中字符串长度统计函数strlen()的实现原理及注意事项,并通过实例演示了如何使用C语言统计输入文本中特定单词的出现次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C语言字符串长度统计函数strlen()的实现原理

C标准库中有一个字符串长度统计函数strlen(),用来统计字符串的长度,它的实现与下面类似。
   
  1. #include <stdlib.h>
  2. size_t strlen( char *string )
  3. {
  4. int length = 0;
  5. // 依次访问字符串的内容,计算字符数,直至遇到NUL终止符
  6. while( *string++ != '\0' )
  7. length += 1;
  8.  
  9. return length;
  10. }
#include <stdlib.h>
size_t strlen( char *string )
{
    int length = 0;
    // 依次访问字符串的内容,计算字符数,直至遇到NUL终止符
    while( *string++ != '\0' )
        length += 1;

    return length;
}
两点注意:
  • size_t 为stddef.h中定义的数据类型,等价于 unsigned int,它的取值必须大于等于0。
  • while()循环条件中,*string++等价于*(string++)。string是指针变量,保存的是字符串的起始地址(第一个字符的地址),地址是一个整数,可以进行算术运算,加 1 后为下一个字符的地址。


在指针到达字符串末尾的NUL字节之前,while语句中*string++表达式的值一直为真。它同时增加指针的值,用于下一次测试。这个表达式甚至可以正确地处理空字符串。

如果这个函数调用时传递给它的是一个NULL指针,那么while语句中的间接访问将会失败。函数是不是应该在解引用指针前检查这个条件?从绝对安全的角度讲,应该如此。但是,这个函数并不负责创建字符串。如果它发现参数为NULL,它肯定发现了一个出现在程序其他地方的错误。 当指针创建时检查它是否有效是合乎逻辑的,因为这样只需检查一次。这个函数采用的就是这种方 法。如果函数失败是因为粗心大意的调用者懒得检查参数的有效性而引起的,那是他活该如此。

 

 

 

 

C语言统计输入的单词的个数

编写一个程序,对标准输入进行扫描,并对单词“the”出现的次数进行计数。进行比较时应该区分大小写,所以“The”和“THE”并不计算在内。你可以认为各单词由一个或多个空格字符分隔,而且输入行在长度上不会超过100个字符。 计数结果应该写到标准输出上。

声明—个长度为101个字节的缓冲区数组,用于保存100个字节的输入和NUL终止符。strtok 函数用于逐个提取单词。
    
  1. // 计算标准输入中单词“the”出现的次数。字母是区分大小写的,输入中的单词由一个或多次空白字符分隔。
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5.  
  6. char const whitespace[] = " \n\r\f\t\v";
  7. int main ()
  8. {
  9. char buffer[101];
  10. int count;
  11. char *word;
  12. count = 0;
  13. // 读入文本行,直到发现EOF
  14. while( gets(buffer) ) {
  15. // 从缓冲区逐个提取单词,直到缓冲区内不再有单词。
  16. for( word = strtok ( buffer, whitespace );
  17. word != NULL;
  18. word = strtok( NULL, whitespace )
  19. ){
  20. if( strcmp( word, "the" ) == 0 )
  21. count += 1;
  22. }
  23. }
  24. printf("%d\n", count);
  25. return EXIT_SUCCESS;
  26. }


 

// // Created by l30079074 on 25-8-11. // #include <securec.h> #include <stdlib.h> #include <stdio.h> #include <stdbool.h> #include “securec.h” static int GetUnselectedNum(const int *favorites,size_t favoritesSize, const int *presents,size_t presentsSize) { int isGetGift[favoritesSize]; memset_s(isGetGift, favoritesSize, 1, favoritesSize); int giftIndex = 0; int peopleIndex = 0; int countOneCycle = 0; int numGetGift = 0; while (peopleIndex < favoritesSize && giftIndex < presentsSize) { if (isGetGift[peopleIndex] == 1 && presents[giftIndex] == favorites[peopleIndex]) { isGetGift[peopleIndex] = -1; giftIndex++; countOneCycle++; numGetGift++; } if (peopleIndex == favoritesSize - 1) { if (countOneCycle == 0) { break; } else { peopleIndex = 0; countOneCycle = 0; } } else { peopleIndex++; } } return favoritesSize - numGetGift; } int main() { int favn = 5; int pren = 5; int fav[5] = {1, 2, 2, 1, 2}; int pre[5] = {2, 1, 1, 2, 1}; int res = GetUnselectedNum(fav, favn, pre, pren); printf(“%d”, res); return 0; } 我这个代码进行如下报错是为什么 ====================[ 构建 | hw663 | Debug ]====================================== "C:\Program Files\JetBrains\CLion 2024.3.2\bin\cmake\win\x64\bin\cmake.exe" --build D:\untitled\cmake-build-debug --target hw663 -j 26 [1/2] Building C object CMakeFiles/hw663.dir/hw663.c.obj In file included from D:/untitled/hw663.c:4: D:/C_Libraries/secure-master/include/securec.h:87:13: warning: 'memmove_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 87 | errno_t memmove_s(void* dest, size_t destMax, const void* src, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:91:13: warning: 'wcscpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 91 | errno_t wcscpy_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:93:13: warning: 'wcsncpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 93 | errno_t wcsncpy_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:95:13: warning: 'wcscat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 95 | errno_t wcscat_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:97:13: warning: 'wcsncat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 97 | errno_t wcsncat_s(wchar_t* strDest, size_t destMax, const wchar_t* strSrc, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:100:11: warning: 'strtok_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 100 | char* strtok_s(char* strToken, const char* strDelimit, char** context); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:102:14: warning: 'wcstok_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 102 | wchar_t* wcstok_s(wchar_t* strToken, const wchar_t* strDelimit, wchar_t** context); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:105:9: warning: 'sprintf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 105 | int sprintf_s(char* strDest, size_t destMax, const char* format, ...) SECUREC_ATTRIBUTE(3,4); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:110:9: warning: 'vsprintf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 110 | int vsprintf_s(char* strDest, size_t destMax, const char* format, va_list argptr) SECUREC_ATTRIBUTE(3,0); | ^~~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:120:9: warning: 'scanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 120 | int scanf_s(const char* format, ...); | ^~~~~~~ D:/C_Libraries/secure-master/include/securec.h:122:9: warning: 'wscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 122 | int wscanf_s(const wchar_t* format, ...); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:130:9: warning: 'fscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 130 | int fscanf_s(FILE* stream, const char* format, ...); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:132:9: warning: 'fwscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 132 | int fwscanf_s(FILE* stream, const wchar_t* format, ...); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:140:9: warning: 'sscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 140 | int sscanf_s(const char* buffer, const char* format, ...); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:142:9: warning: 'swscanf_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 142 | int swscanf_s(const wchar_t* buffer, const wchar_t* format, ...); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:150:11: warning: 'gets_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 150 | char* gets_s(char* buffer, size_t destMax); | ^~~~~~ D:/C_Libraries/secure-master/include/securec.h:155:13: warning: 'memcpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 155 | errno_t memcpy_s(void* dest, size_t destMax, const void* src, size_t count); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:158:13: warning: 'strcpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 158 | errno_t strcpy_s(char* strDest, size_t destMax, const char* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:160:13: warning: 'strncpy_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 160 | errno_t strncpy_s(char* strDest, size_t destMax, const char* strSrc, size_t count); | ^~~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:163:13: warning: 'strcat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 163 | errno_t strcat_s(char* strDest, size_t destMax, const char* strSrc); | ^~~~~~~~ D:/C_Libraries/secure-master/include/securec.h:165:13: warning: 'strncat_s' redeclared without dllimport attribute: previous dllimport ignored [-Wattributes] 165 | errno_t strncat_s(char* strDest, size_t destMax, const char* strSrc, size_t count); | ^~~~~~~~~ [2/2] Linking C executable hw663.exe FAILED: hw663.exe C:\Windows\system32\cmd.exe /C "cd . && C:\PROGRA~1\JETBRA~1\CLION2~1.2\bin\mingw\bin\gcc.exe -g CMakeFiles/hw663.dir/hw663.c.obj -o hw663.exe -Wl,--out-implib,libhw663.dll.a -Wl,--major-image-version,0,--minor-image-version,0 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid -lcomdlg32 -ladvapi32 && cd ." C:\Program Files\JetBrains\CLion 2024.3.2\bin\mingw\bin/ld.exe: CMakeFiles/hw663.dir/hw663.c.obj: in function `GetUnselectedNum': D:/untitled/hw663.c:13: undefined reference to `memset_s' collect2.exe: error: ld returned 1 exit status ninja: build stopped: subcommand failed.
08-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值