#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
#if 0
char str[256];
// strcpy(str, "Eric hao ");
strncpy(str, "Eric hao ", 4);
strcat(str, "is a king! Hahahaha\n");
printf("String funciton %s", str);
strncat(str, "Yess, heihei", 6);
printf("String funciton %s\n", str);
char str1[256] = "Eric hao";
printf("String compare %s and %s result %d\n", str, str1, strcmp(str,str1));
#endif
#if 1
char* charPos;
char str[256] = "This is an example string";
charPos = (char*)memchr(str, 'p', strlen(str));
if(NULL != charPos)
{
printf("Char %c found in %s, position at %d\n", *charPos, str, (charPos - str +1));
}else
{
printf("Char %c is not found\n");
}
charPos = strchr(str, 's');
while(NULL != charPos)
{
printf("Find parameter \' s \' is at postion %d\n", (charPos - str + 1));
charPos = strchr(charPos+1, 's');
}
char strby[256] = "This is -- split the string -- example --- eric";
char split[4] = "---";
char* token;
token = strtok(strby, split);
while(NULL != token)
{
printf("%s\n", token);
token = strtok(NULL, split);
}
#endif
system("pause");
return 0;
}
<cstring> string.h
于 2022-01-22 10:31:05 首次发布