用思维导图和实例学习C++之一——字符串处理

来源:互联网 发布:mysql去重复 保留一条 编辑:程序博客网 时间:2024/06/06 10:48

用思维导图和实例学习C++之一——字符串处理 收藏 此文于2010-05-17被推荐到CSDN首页

 

原帖地址: http://blog.csdn.net/gnuhpc/archive/2010/05/17/5599102.aspx


如何被推荐?
1.本章思维导图:

 

 

 

Example1:
char *strcpy(char *target, const char *source) {
  char *t = target;
  // Copy the contents of source into target.
  while(*source) *target++ = *source++;
  // Null-terminate the target.
  *target = '/0';
  // Return pointer to the start of target.
  return t;
}

Example2:
void *memmove(void *target, const void *source, size_t count)
这个函数即使是在源和目的字符串有所重叠时操作也能成功,虽然source为const,但是其指向的array也可能被修改。

2. C型字符串操作实例:

Ex1.基本操作

/*
* =====================================================================================
*
*       Filename:  2-1.cpp
*
*    Description:  Fundamental Operations in C Type String
*
*        Version:  1.0
*        Created:  05/11/2010 10:43:11 AM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/
#include 
#include 

using namespace std;

int main(int argc, char *argv[])
{
    char strA[7]="UP";
    char strB[5]="DOWN";
    char strC[5]="LEFT";
    char strD[6]="RIGHT";

    /*Display */
    cout << "Here are the strings: " << endl;
    cout << "strA: " << strA << endl;
    cout << "strB: " << strB << endl;
    cout << "strC: " << strC << endl;
    cout << "strD: " << strD << "/n/n";

    //Display the length of strA.
      cout << "Length of strA is " << strlen(strA) << endl;
      cout << "Size of strA is " << sizeof(strA) << endl;
    //Concatenate strB with strA
    cout << "The result of Concatenate is strA::" <

    //Copy strC into strB,and partially strD into strA
    cout << "The result of Copy is:" <    cout << "The result of partially Copy is strA:" <

    //Compare strC with strB
    if( !strcmp(strC,strB))
    {
        cout << "strC is equal to strB!"<    }

    if( !strncmp(strD,strA,3))
    {
        cout << "strD is equal to strA partially!"<    }

    return 0;

}

Ex2.搜索匹配相关操作

/*
* =====================================================================================
*
*       Filename:  2-2.cpp
*
*    Description:  Search Operation in C type String
*
*        Version:  1.0
*        Created:  05/11/2010 11:38:15 AM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/

#include
#include
using namespace std;
int main(void) {
  const char *url = "HerbSchildt.com";
  const char *url2 = "Apache.org";
  const char *emailaddr = "Herb@HerbSchildt.com";
  const char *tld[] = { ".com", ".net", ".org" };
  const char *p;
  // First, determine if url and url2 contain .com, .net, or .org.
  for(int i=0; i < 3; i++) {
    p = strstr(url, tld[i]);
    if(p) cout << url << " has top-level domain " << tld[i] << endl;
    p = strstr(url2, tld[i]);
    if(p) cout << url2 << " has top-level domain " << tld[i] << endl;
  }
  // Search for a specific character.
  p = strchr(emailaddr, '@');
  if(p) cout << "Site name of e-mail address is: " << p+1 << endl;
  // Search for any of a set of characters. In this case,
  // find the first @ or period.
  p = strpbrk(emailaddr, "@.");
  if(p) cout << "Found " << *p << endl;

  //Search for the beginning
  if(strspn(url2,"Apache")==6){
    cout<< "Url2 begins with the /"Apache/""<< endl;
  }

  //与strchr正好倒过来
  if(p=strrchr(emailaddr, 'b')){
    cout<< p << endl;
  }
  return 0;
}

Ex3.倒置一个字符串

我们用了四种方法,最后一种方法经常在面试笔试题中出现。

/*
* =====================================================================================
*
*       Filename:  2-3.cpp
*
*    Description:  Reverse a string
*
*        Version:  1.0
*        Created:  05/12/2010 03:08:09 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/
#include
#include
using namespace std;
void revstr(char *str);
void revstr_p(char *str);
void revstr_recursive(char *str, int start, int end);
char *(revstrcpy)(char *rstr, const char *orgstr);
int main() {
  char str[] = "abcdefghijklmnopqrstuvwxyz";
  char *des= new char[27];
  cout << "Original string: " << str << endl;
  revstr(str);
  cout << "Reversed string: " << str << endl;
  revstr_p(str);
  cout << "Reversed string using pointer: " << str << endl;
  revstr_recursive(str,0,strlen(str)-1);
  cout << "Reversed string using recursive: " << str << endl;
  cout << "Reversed string using copy method: " << revstrcpy(des,str)<< endl;
  return 0;
}

void revstr(char *str) {
  int i, j;
  char t;
  for(i = 0, j = strlen(str)-1; i < j; ++i, --j) {
    t = str[i];
    str[i] = str[j];
    str[j] = t;
  }
}

// Reverse a string in place. Use pointers rather than array indexing.
void revstr_p(char *str) {
  char t;
  char *inc_p = str;
  char *dec_p = &str[strlen(str)-1];
  while(inc_p <= dec_p) {
    t = *inc_p;
    *inc_p++ = *dec_p;
    *dec_p-- = t;
  }
}

void revstr_recursive(char *str, int start, int end) {
  if(start < end)
    revstr_recursive(str, start+1, end-1);
  else
    return;
  char t = str[start];
  str[start] = str[end];
  str[end] = t;
}

char *(revstrcpy)(char *rstr, const char *orgstr) { //返回rstr的原始值使函数能够支持链式表达式,增加了函数的“附加值”。同样
    功能的函数,如果能合理地提高的可用性,自然就更加理想。

  if((rstr==NULL)||(orgstr==NULL))
      throw "Invalid argument(s)";

  char *dst=rstr;
  dst += strlen(orgstr);
  *dst-- = '/0';
  while(*orgstr)  *dst-- = *orgstr++; 
  return rstr;
}

Ex4.忽略大小写的字符串比较

/*
* =====================================================================================
*
*       Filename:  2-4.cpp
*
*    Description:  Ignore the Letter case when compared
*
*        Version:  1.0
*        Created:  05/12/2010 03:44:34 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/

#include
#include
using namespace std;
int strcmp_ign_case(const char *str1, const char *str2);
int main(void) {
  char strA[]= "tesT";
  char strB[] = "Test";
  char strC[] = "testing";
  char strD[] = "Tea";
  int result;
  cout << "Here are the strings: " << endl;
  cout << "strA: " << strA << endl;
  cout << "strB: " << strB << endl;
  cout << "strC: " << strC << endl;
  cout << "strD: " << strD << "/n/n";
  // Compare strings ignoring case.
  result = strcmp_ign_case(strA, strB);
  result = strcmp_ign_case(strA, strC);
  result = strcmp_ign_case(strA, strD);
  result = strcmp_ign_case(strD, strA);
  return 0;
}
// A simple string comparison function that ignores case differences.
int strcmp_ign_case(const char *str1, const char *str2) {
  const char *str1_cp=str1;
  const char *str2_cp=str2;
  while(*str1_cp && *str2_cp) {
    if(tolower(*str1_cp++) != tolower(*str2_cp++)) 
      break;
  }
  int result=tolower(*str1_cp) - tolower(*str2_cp);
  cout << str1 << " is ";
  if(!result)
    cout << "equal to ";
  else if(result < 0)
    cout << "less than ";
  else 
    cout << "greater than ";
  cout << str2 << endl;
  return result;
}

Ex5.搜索替换

/*
* =====================================================================================
*
*       Filename:  2-5.cpp
*
*    Description:  Replace the sub_str with another str
*
*        Version:  1.0
*        Created:  05/12/2010 04:07:02 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/

#include
#include
#include
using namespace std;
bool search_and_replace(char *orgstr, int maxlen,
                        const char *oldsubstr, const char *newsubstr);
char *search_and_replace_alloc(const char *str, const char *oldsubstr,
                               const char *newsubstr) throw(bad_alloc);
int main(void) {
    char str[80] = "alpha beta gamma alpha beta gamma";
    char *ptr=NULL;
    cout << "Original string: " << str << "/n/n";
    cout << "First, replace all instances of alpha with epsilon./n";
  // Replace all occurrences of alpha with epsilon.
    while(search_and_replace(str, sizeof(str), "alpha", "epsilon"))
        cout << "After a replacement: " << str << endl;
    cout << "Second, replace one instances of epsilon with alpha ./n";

    try {
      ptr = search_and_replace_alloc(str, "epsilon", "alpha");
    } catch(bad_alloc exc) {
      // Take appropriate action here.
    }
    if(ptr) {
      cout << "After a replacement: " << ptr << endl;
      delete [] ptr;
    }

    return 0;
}
bool search_and_replace(char *orgstr, int maxlen,
                        const char *oldsubstr, const char *newsubstr)
{
    char *pos=NULL;
    if((orgstr==NULL)||(oldsubstr==NULL)||(newsubstr==NULL))
        return false;       
    int len = strlen(orgstr) - strlen(oldsubstr) + strlen(newsubstr);
    if(len > maxlen)
        return false;
    if(pos=strstr(orgstr,oldsubstr)){
        memmove(pos+strlen(newsubstr),pos+strlen(oldsubstr),strlen(pos)-strlen(oldsubstr)+1);       
        strncpy(pos, newsubstr, strlen(newsubstr));
        return true;
    }
    else{
        return false;
    }
}

char *search_and_replace_alloc(const char *str, const char *oldsubstr,
                               const char *newsubstr) throw(bad_alloc) {
    const char *pos=NULL;
    if((str==NULL)||(oldsubstr==NULL)||(newsubstr==NULL))
        return NULL;       
    int size = strlen(str) + strlen(newsubstr) - strlen(oldsubstr) + 1;
    char *result = new char[size];
    if(pos=strstr(str,oldsubstr)){
         strncpy(result, str, pos-str);
         *(result+(pos-str)) = '/0';
         strcat(result, newsubstr);
         strcat(result, pos+strlen(oldsubstr));
         cout<         return result;
    }
    else {
        delete [] result;
        return NULL;
    }
}

Ex6.文本统计

/*
* =====================================================================================
*
*       Filename:  2-6.cpp
*
*    Description:  Text statistics Function
*
*        Version:  1.0
*        Created:  05/13/2010 02:37:34 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/

#include
#include
using namespace std;

struct wc { //不使用class是因为这是一个纯数据的对象,虽然它包含一个默认的构造函数。
  int words;
  int spaces;
  int punct;
  int lines;
  wc() {
    words = punct = spaces = lines = 0;
  }
};
wc wordcount(const char *str);
int main() {
  const char *test = "By supplying a string class and also "
               "supporting null-terminated strings,/nC++ "
               "offers a rich programming environment for "
               "string-intensive tasks./nIt's power programming.";
  cout << "Given: " << "/n/n";
  cout << test << endl;
  wc wcd = wordcount(test);
  cout << "/nWords: " << wcd.words << endl;
  cout << "Spaces: " << wcd.spaces << endl;
  cout << "Lines: " << wcd.lines << endl;
  cout << "Punctuation: " << wcd.punct << endl;
  return 0;
}
wc wordcount(const char *str) {
  wc data;  

  if(*str) ++data.lines;
  while(*str) { 

    if(isalpha(*str)) { 
      while(isalpha(*str) || *str == '/'') {
        if(*str == '/'') ++data.punct;
        ++str;
      }
      data.words++; 
    }
    else { 
      if(ispunct(*str)) ++data.punct;
      else if(isspace(*str)) {
        ++data.spaces; 
        if(*str == '/n' && *(str+1)) ++data.lines;
      }
      ++str;
    }
  }
  return data;
}

Ex7 解析一个C型字符串

/*
* =====================================================================================
*
*       Filename:  2-7.cpp
*
*    Description:  String Token
*
*        Version:  1.0
*        Created:  05/14/2010 10:01:58 AM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/

#include
#include
using namespace std;

#define     MAX_TOKEN_SIZE 128
const char *gettoken(const char *str);
int main() {
  char delims[] = "., ?;!";
  char str[] = "I like apples, pears, and grapes. Do you?";
  char *tok;
  cout << "Obtain the words in a sentence./n";
  tok = strtok(str, delims);
  while(tok) {
    cout << tok << endl;
    tok  = strtok(NULL, delims);
  }
  char kvpairs[] = "count=10, name=/"Tom Jones, jr./", max=100, min=0.01";
  char kvdelims[] = " =,";
  cout << "/nTokenize key/Value pairs./n";
  tok = strtok(kvpairs, kvdelims);
  while(tok) {
    cout << "Key: " << tok << " ";
    if(!strcmp("name", tok)) {
      tok = strtok(NULL, "/"");
    }
    else {
      tok  = strtok(NULL, kvdelims);
    }
    cout << "Value: " << tok << endl;
    tok = strtok(NULL, kvdelims);
  }

  cout <<"Ori String is " << kvpairs  <

  //We want to token the count ,12 and the symbol add(+),but we cannot make it via strtok
  char count[]="max=12+3/89; count27 = 19*(min+floor);";
  char countdelims[]="=+";

  const char *strtok = gettoken(count);
  while(strtok) {
    cout << strtok << endl;
    strtok  = gettoken(NULL);
  }
  cout << "/n/n";
  return 0;
}

const char *gettoken(const char *str) {
  static char token[MAX_TOKEN_SIZE+1]; //static makes the return method can be made.
  static const char *ptr; //static type holds the string last time passed in
  int count= 0; // holds the current character count
  char *tokptr=token;
  if(str) {
    ptr = str;
  }
  while(isspace(*ptr)) ptr++;
  if(isalpha(*ptr)) {
    while(isalpha(*ptr) || isdigit(*ptr)) {
      *tokptr++ = *ptr++;
      ++count;
      if(count == MAX_TOKEN_SIZE) break;
    }
  } else if(isdigit(*ptr)) {
    while(isdigit(*ptr)) {
      *tokptr++ = *ptr++;
      ++count;
      if(count == MAX_TOKEN_SIZE) break;
    }
  } else if(ispunct(*ptr)) {
    *tokptr++ = *ptr++;
  } else return NULL;
  // Null terminate the token.
  *tokptr = '/0';
  return token; 
}

3. String操作实例:

Ex8 String基本操作

/*
* =====================================================================================
*
*       Filename:  2-8.cpp
*
*    Description:  String Basic Operation
*
*        Version:  1.0
*        Created:  05/14/2010 02:15:06 PM
*       Revision:  none
*       Compiler:  gcc
*
*         Author:  gnuhpc (http://blog.csdn.net/gnuhpc), warmbupt@gmail.com
*        Company:  IBM CDL
*
* =====================================================================================
*/

#include
#include
using namespace std;
int main()
{
  // Create some string objects. Three are initialized
  // using the string literal passed as an argument.
  string str1("Alpha");
  string str2("Beta");
  string str3("Gamma");
  string str4;
  // Output a string via cout.
  cout << "Here are the original strings:/n";
  cout << "  str1: " << str1 << endl;
  cout << "  str2: " << str2 << endl;
  cout << "  str3: " << str3 << "/n/n";
  // Display the maximum string length.
  cout << "The maximum string length is: " << str1.max_size()
       << "/n/n";
  // Display the size of str1. You can use length() instead.这两个方法其实是一个效果,有size只是为了满足兼容STL的要求
  cout << "str1 contains " << str1.size() << " characters./n";
  // Display the capacity of str1.
  cout << "Capacity of str1: " << str1.capacity() << "/n/n";
  // Display the characters in a string one at a time
  // by using the indexing operator.
  for(unsigned i = 0; i < str1.size(); ++i)
 &