面试宝典:转换字符串格式为原来字符串里的字符+该字符连续出现的个数

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;

int main()
{

  char s[50]={'\0'};
  char str[30]="1233422222";

  int len = strlen(str);

  for( int i = 0; i < len; ++i )
  {
    int count = 1;

    while( str[i] == str[ i + 1 ] )
  {
    i++;
    count++;
  }

    sprintf( s+strlen(s) , "%c%d", str[i], count);
  }

  s[ strlen(s) ] = '\0';
  cout<<s<<endl;
  system("pause");

  return 0;
}

posted @ 2013-04-08 13:55  NinaGood  阅读(249)  评论(0编辑  收藏  举报