设为首页 加入收藏

TOP

4.5.5 搜索以空字符结尾的字符串
2013-10-07 12:28:52 来源: 作者: 【 】 浏览:66
Tags:4.5.5 搜索 字符 结尾 字符串

4.5.5  搜索以空字符结尾的字符串

strspn()函数可以搜索字符串中不包含在给定集合中的第一个字符,并返回该字符的索引。第一个参数是指向要搜索的字符串的指针,第二个参数是指向包含该字符集合的字符串的指针。

  1. char* str = "I agree with everything.";  
  2. char* vowels = "aeiouAEIOU ";  
  3. size_t index = strspn(str, vowels);  
  4. cout << "The first character that is not a vowel is '" << str[index]  
  5. << "' at position " << index << endl

这段代码在str中搜索第一个字符不在vowels中的字符串。注意,vowels集合中包括一个空格,因此在搜索时会忽略空格。这段代码的输出为:

The first character that is not a vowel is 'g' at position 3

strspn()函数的返回值表示第一个参数字符串落在第二个参数字符串中的子串长度。在本例中该子串是"I a"。

wcspn()函数是strspn()的宽字符串版本。

strstr()函数返回一个指针,指向第一个参数中第二个参数指定的子串的位置。下面的代码显示了运行中的strstr()函数:

  1. char* str = "I agree with everything.";  
  2. char* substring = "ever";  
  3. char* psubstr = strstr(str, substring);  
  4.  
  5. if(!psubstr)  
  6. cout << "\"" << substring << "\" not found in \"" << str << "\"" << endl;  
  7. else  
  8. cout << "The first occurrence of \"" << substring 
  9. << "\" in \"" << str << "\" is at position "  
  10. << psubstr-str << endl

第三个语句调用strstr()函数,在str中搜索子串第一次发生的位置。该函数返回一个指向子串位置的指针(如果找到了),如果没有找到,则返回NULL。if语句根据有没有在str中找到substring返回一条消息。表达式psubstr-str给出了子串中第一个字符的索引位置。这段代码产生的输出是:

  1. The first occurrence of "ever" in "I agree with everything." is at position 13 

试一试  搜索以空字符结尾的字符串

本示例搜索一个给定的字符串来确定给定子串出现的次数。代码如下:

  1. //Ex4_12.cpp  
  2. // Searching a string  
  3. #include <iostream> 
  4. #include <cstring> 
  5. using std::cout;  
  6. using std::endl;  
  7. using std::strlen;  
  8. using std::strstr;  
  9.  
  10. int main()  
  11. {  
  12. char* str = "Smith, where Jones had had \"had had\" had had \"had\"."  
  13. "\n\"Had had\" had had the examiners' approval.";  
  14.  
  15. char* word = "had";  
  16. cout << "The string to be searched is: "  
  17. << endl << str << endl;  
  18.  
  19. int count = 0;                                 
    // Number of occurrences of word in str  
  20. char* pstr = str;                       
    // Pointer to search start position  
  21. char* found = 0;                           
    // Pointer to occurrence of word in str  
  22.  
  23. while(true)  
  24. {  
  25. found = strstr(pstr, word);  
  26. if(!found)  
  27. break;  
  28. ++count;  
  29. pstr = found+strlen(word);  
  30.                  // Set next search start as 1 past the word found  
  31. }  
  32.  
  33. cout << "\"" << word << "\" was found "  
  34. << count << " times in the string." << endl;  
  35. return 0;  

这个示例的输出为:

  1. The string to be searched is: Smith, 
    where Jones had had "had had" had had "had".  
  2. "Had had" had had the examiners' approval.  
  3. "had" was found 10 times in the string. 

示例说明

所有动作都发生在不确定的while循环中:

  1. while(true)  
  2. {  
  3. found = strstr(pstr, word);  
  4. if(!found)  
  5. break;  
  6. ++count;  
  7. pstr = found+strlen(word);  
  8.                         // Set next search start as 1 past the word found  

第一步是在字符串中从位置pstr开始搜索word,它最初是字符串的开头。我们将strstr()返回的地址存储在found中;如果在str中没有找到word,返回值就为空值。因此,在这种情况下if语句会结束循环。

如果found不为空,我们就递增word的出现次数,并更新pstr指针,使它指向在str中找到的word实例后面的那个字符。它将是在下一个循环迭代上搜索的起点。

从输出中可以看出在str中找到了10次word。当然"Had"不计数,因为它是大写字母开头的。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇4.5.1 查找以空字符结尾的字符串.. 下一篇4.5.4 比较以空字符结尾的字符串

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: