C# c语言中的字符串压缩

C# c语言中的字符串压缩,c#,C#,我有一个c程序,它用c语言成功地进行了字符串压缩,在c语言中是一种蛮力方法。例如,如果输入是AABCCCCDDDDA,那么输出应该是a2b1c3d11a2 我用c语言解决了这个问题,方法是取每个字符并计算其出现的次数,然后打印该字符及其计数 我正在尝试将其转换为c语言。我想知道在c语言中应该很容易做到,因为有这么多内置的string和char方法 在c#中有没有一种方法可以做到这一点,即在很少的几行中使用lambda表达式或内置的string或char类型的方法 我的C代码是:

我有一个c程序,它用c语言成功地进行了字符串压缩,在c语言中是一种蛮力方法。例如,如果输入是AABCCCCDDDDA,那么输出应该是a2b1c3d11a2

我用c语言解决了这个问题,方法是取每个字符并计算其出现的次数,然后打印该字符及其计数

我正在尝试将其转换为c语言。我想知道在c语言中应该很容易做到,因为有这么多内置的string和char方法

在c#中有没有一种方法可以做到这一点,即在很少的几行中使用lambda表达式或内置的string或char类型的方法

我的C代码是:

        char *encode(char *src)
    {     
          int recurringLen;
          char count[MAX_RLEN];
          char *dest;
          int i, j = 0, k;
          int len = strlen(src);

         // _itoa_s(34,c,10);
          /* If all characters in the source string are different, 
            then size of destination string would be twice of input string.
            For example if the src is "abcd", then dest would be "a1b1c1d1"
            For other inputs, size would be less than twice. 
            test for the scenarios where abababababababababababa bcos output here is a16b11.
            aabbbcccd
            */
           dest = (char *)malloc(sizeof(char)*(len*2 + 1));


          /* traverse the input string one by one */
          for(i = 0; i < len; i++)
          {

            /* Copy the first occurrence of the new character */
            dest[j++] = src[i];

            /* Count the number of occurrences of the new character */
            recurringLen = 1;     
            while(i + 1 < len && src[i] == src[i+1])
            {
              recurringLen++;
              i++;
            }   

            /* Store rLen in a character array count[] */
            sprintf_s(count, "%d", recurringLen);

            /* Copy the count[] to destination */
            for(k = 0; *(count+k); k++, j++)
            { 
              dest[j] = count[k]; 
            } 
          }  

          /*terminate the destination string */
          dest[j] = '\0';
          return dest;
    }     
char*encode(char*src)
{     
int recurringLen;
字符数[MAX_RLEN];
char*dest;
int i,j=0,k;
int len=strlen(src);
//伊图阿(34,c,10);
/*如果源字符串中的所有字符都不同,
那个么目标字符串的大小将是输入字符串的两倍。
例如,如果src为“abcd”,则dest为“a1b1c1d1”
对于其他输入,大小将小于两倍。
测试AbabababaBCOS输出为a16b11的场景。
aabbbcccd
*/
dest=(char*)malloc(sizeof(char)*(len*2+1));
/*逐个遍历输入字符串*/
对于(i=0;i
通过编写扩展方法
GroupSeqsBy

string input= "aabccccdddddddddddaa";
var s = String.Join("",input.GroupSeqsBy(c => c)
                            .Select(g => g.Key.ToString() + g.Value.Count()));

public static IEnumerable GroupSeqsBy(此IEnumerable列表,Func keySelector)
{
List retList=新列表();
S prev=keySelector(list.FirstOrDefault());
foreach(列表中的T项)
{
if(键选择器(项).Equals(上一个))
重新列出。添加(项目);
其他的
{
返回新的KeyValuePair(prev,retList);
prev=按键选择器(项目);
retList=新列表();
重新列出。添加(项目);
}
}
如果(retList.Count>0)
返回新的KeyValuePair(prev,retList);
}

使用正则表达式可以做到这一点(假设您的示例有一个输入错误,其中c3应该是c4)

输出为:

a2b1c4d11a2

基本上,我们正在搜索任何重复0次或更多次的字符,然后将其替换为后跟匹配字符串长度的字符

具体而言:

  • 。是任何字符的匹配项(除\n之外)
  • (.)圆括号构成一个圆括号
  • \1是该组的一个字符,基本上重复使用已匹配的字符
  • *是一句谚语,重复匹配0次或更多次。我们也可以使用{0,}
以下是一些有用的链接: || | | |

如果希望这是字符串的扩展(不确定这是否是一个要求),那么:

使用方法如下:

string theString = "aabccccdddddddddddaa";
string result = theString.Compress();

显示你的
C
代码,告诉人们你卡在哪里了。我的C代码工作得很好。我只是想把它转换成C#,想知道它能做得更优雅吗?为什么在这个问题上投反对票???输出不应该是
a2bc4d11a2
而不是
a2bc3d11a2
?实际上,根据源代码中的评论,它应该是
a2b1c4d11a2
,不是吗?你可以返回
I分组
你不这样认为吗?你能再多描述一点吗?我已经添加了一点,和一些链接。我还对其进行了更改,以便在任何字符(.)上进行匹配,而不仅仅是单词(w)字符,因为这就是您的示例所做的。
static readonly Regex re = new Regex( @"(.)\1*", RegexOptions.Compiled );               
static void Main()
{
    string result = re.Replace( "aabccccdddddddddddaa", match => match.Value[0] + match.Length.ToString() );                        
    Console.WriteLine( result );
}
public static class StringExtensions
{ 
    static readonly Regex re = new Regex( @"(.)\1*", RegexOptions.Compiled );                
    public static string Compress(this string theString)
    {
        return re.Replace( theString, match => match.Value[0] + match.Length.ToString() );             
    }
}
string theString = "aabccccdddddddddddaa";
string result = theString.Compress();