// 字串查找.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <assert.h> using namespace std; //字符串中,插入字符串统计的个数 char *transformation(const char *str){ assert(str!=NULL);//判断是否为空指针 static char a[20]; const char *p=str; char *buf=new char[8]; int count=1,i=0,len=0; while (*(p+i)!='\0') { count=1; len=i; while (*(p+i)==*(p+i+1)) //如果是相等字符,则count统计相等个数 { i++; count++; } itoa(count,buf,10);//把个数转换为字符串存入buf中 strncat(a,p+len,count);//把相等的这些字符追加到数组a中 strcat(a,buf);//把个数追加到数组a的后面 a[strlen(a)]='\0';//数组末尾加上结束符 i++;//变量自增 } delete [] buf;//释放空间 return a;//返回字符串 } int _tmain(int argc, _TCHAR* argv[]) { char p1[]="aaaabbccceefgkl"; char *p2=transformation(p1); cout<<p2<<endl; system("pause"); return 0; }