例342:字符串大小转换函数strlwr()和strupr()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
// strlwr将字符串中的S参数转换为小写形式
// strupr将字符串中的S参数转换为大写形式
// strlwr和strupr不是标准C库函数,只能在VC中使用。linux gcc环境下需要自行定义这个函数
printf(strlwr("1001 C/C++ Tips!\n"));
printf(strupr("1001 C/C++ Tips!\n"));
return 0;
}
例343:字符串连接函数strcat()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char name[64] = "Triggerhill's I'm so";
strcat(name, " Happy");
printf("Happy's full name is %s\n", name); // Happy's full name is Triggerhill's I'm so Happy
return 0;
}
例344:字符串中查找字符函数strchr()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char title[64] = "1001 C/C++ Tips!";
char *ptr;
// strchr在参数str所指向的字符串中搜索第一次出现字符c(一个无符号字符)的指针
if (ptr = strchr(title, 'C'))
printf("First occurrence of C is at offset %d\n", ptr - title);// First occurrence of C is at offset 5
else
printf("Character not found\n");
return 0;
}
例345:字符串比较函数strcmp()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
printf("Comparing Abc with Abc %d\n", strcmp("Abc", "Abc")); // Comparing Abc with Abc 0
printf("Comparing abc with Abc %d\n", strcmp("abc", "Abc")); // Comparing abc with Abc 1
printf("Comparing abcd with abc %d\n", strcmp("abcd", "abc")); // Comparing abcd with abc 1
printf("Comparing Abc with Abcd %d\n", strcmp("Abc", "Abcd")); // Comparing Abc with Abcd -1
return 0;
}
例346:字符串复制函数strcpy()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char title[] = "Jamsa's 1001 C/C++ Tips";
char book[128];
strcpy(book, title);
printf("Book name %s\n", book); // Book name Jamsa's 1001 C/C++ Tips
return 0;
}
例347:字符串复制函数strdup()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char *title;
// strdup将串拷贝到新建的位置处, 内部使用malloc(), 不用时要用free
if ((title = strdup("Jamsa's 1001 C/C++ Tips")))
printf("Title: %s\n", title);
else
printf("Error duplicating string");
return 0;
}
例348:自定义函数比较两个字符串是否相等
#include <stdio.h>
/**
* 比较两个字符串是否相等
* 相等返回1, 否则返回0
*/
int streql(char *str1, char *str2)
{
while ((*str1 == *str2) && (*str1))
{
str1++;
str2++;
}
return((*str1 == NULL) && (*str2 == NULL));
}
int main(int argc, char const *argv[])
{
printf("Testing Abc and Abc %d\n", streql("Abc", "Abc")); // Testing Abc and Abc 1
printf("Testing abc and Abc %d\n", streql("abc", "Abc")); // Testing abc and Abc 0
printf("Testing abcd and abc %d\n", streql("abcd", "abc")); // Testing abcd and abc 0
return 0;
}
例349:自定义函数比较两个字符串是否相等(不区分大小写)
#include <stdio.h>
#include <ctype.h>
/**
* 不区分大小写比较字符串是否相等
*/
int strieql(char *str1, char *str2)
{
while ((toupper(*str1) == toupper(*str2)) && (*str1))
{
str1++;
str2++;
}
return((*str1 == NULL) && (*str2 == NULL));
}
int main(int argc, char const *argv[])
{
printf("Testing Abc and Abc %d\n", strieql("Abc", "Abc")); // Testing Abc and Abc 1
printf("Testing abc and Abc %d\n", strieql("abc", "Abc")); // Testing abc and Abc 1
printf("Testing abcd and abc %d\n", strieql("abcd", "abc")); // Testing abcd and abc 0
return 0;
}
例350:自定义函数查找字符串中的字符下标
#include <stdio.h>
/**
* 返回字符串中指定字符的下标, 没有就返回'\0'的下标, 即字符串的长度
*/
int str_index(const char *string, int letter)
{
char *original = (char *)string;
while ((*string != letter) && (*string))
string++;
return(string - original);
}
int main(int argc, char const *argv[])
{
printf("Location of C is %d\n", str_index("1001 C/C++", 'C'));// Location of C is 5
printf("Location of x is %d\n", str_index("1001 C/C++", 'x'));// Location of x is 10
return 0;
}
例351:字符串长度函数strlen()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char book_title[] = "Jamsa's 1001 C/C++ Tips";
printf("%s contains %d characters\n", book_title, strlen(book_title));// Jamsa's 1001 C/C++ Tips contains 23 characters
return 0;
}
例352:字符串连接函数strncat()
#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
char name[64] = "Bill";
// char * strncat(char *dest, const char *src, size_t n);
// 在dest的结尾追加src的前n个字符
strncat(name, " and Hillary", 4);
printf("Did you vote for %s?\n", name); // Did you vote for Bill and?
return 0;
}