4

大きな文字列 char myStr="AAAABBBCCCCCCDDDEFGHHIJJ" があります。この文字列を文字列圧縮関数に渡します。この関数は、次の形式で文字列を返す必要があります。一時配列を作成することはできません。

以下は私の機能であり、重複の削除と同じ文字列内のそのカウントへの置き換えを理解できません。

 #include<stdio.h>
 #include<string.h>


 char* StrCompress(char myStr[])
 {
char *s = myStr;
int len = strlen(myStr);
char *in = myStr;
int count =0;
int i=0;


while(*(s) != '\0')
{
    if(*(s)==*(s+1))
    {
        count++;

        if(count == 1)
        {
            in = s;
        }
        s++;

    }
    else
    {
        //myStr[count-1]=count;
        memcpy(in+1,s+1,count);
        s=in;
        count =0;

    }
    i++;
}

return myStr;



}

int main(){

char myStr[] ="AAAABBBCCCCCEEFGIIJJJKLMNNNNOOO";

printf("Compressed String is : %s\n",StrCompress(&myStr));

return 0;

}
4

13 に答える 13

4

わずかに変更されたバージョン:

char* StrCompress(char myStr[])
{
  char *s, *in;
  for (s = myStr, in = myStr; *s; s++) {
    int count = 1;
    in[0] = s[0]; in++;
    while (s[0] == s[1]) {
      count++;
      s++;
    }   
    if (count > 1) {
      int len = sprintf(in, "%d", count);
      in += len;
    }   
  }
  in[0] = 0;
  return myStr;
}

さらに、配列名で呼び出す場合は、演算子のアドレスを使用しないでください。

StrCompress(myStr); // not StrCompress(&myStr)

キャラクターが9回以上繰り返すことができないと仮定している場合は、in[0] = '0' + count代わりに次のsprintfものを使用できます。

if (count > 1) {
  in[0] = '0' + count;
  in++;
}   
于 2012-12-26T07:37:29.767 に答える
2
#include<stdio.h>

char* StrCompress(char myStr[])
{
    char *s = myStr;
    char *r, *p;
    int count, i;

    while (*s)
    {
        /*initially only 1 character of a kind is present*/
        count = 1;

        /*we check whether current character matches the next one*/
        while (*s && *s == *(s+1))
        {
            /*if yes,then increase the count due to the match 
            and increment the string pointer to next */
            count++;
            s++;
        }

        if (count > 1) /*if more than one character of a kind is present*/
        {
            /*assign the value of count to second occurence of a particular character*/
            *(s - count + 2) = count + '0';

            /*delete all other occurences except the first one and second one using array shift*/
            for (i = 0; i < count - 2; i++)
            {
                p = s + 1;
                r = s;

                while (*r)
                    *r++ = *p++;

                s--;
            }
        }
        s++;
    }

    return myStr;
}

int main()
{
    char myStr[] = "AAAABBBCCCCCCDDDEFGHHIJJ";

    printf("Compressed String is : %s\n", StrCompress(myStr));

    return 0;
}
于 2012-12-26T08:15:22.257 に答える
0

以下は、誰かがそれを必要とする場合の別の実装です。参考までに、この方法はランレングス符号化と呼ばれます

#include <iostream>

void CompressString (std::string str)
{
    //count will keep track of the number of occurences of any given character
    unsigned int count = 1;

    //new string to store the values from the original string
    std::string str2 = "";

    //store the first letter of the string initially
    char ch = str[0];

    //run a loop from the second character of the string since first character if stored in "ch"
    for (unsigned int i = 1; i < str.length(); i++)
    {
        if (str[i] == ch)
            count++;
        else
        {
            str2 = str2 + ch + std::to_string (count);
            ch = str[i];
            count = 1;
        }
    }

    //for cases like aabbb
    str2 = str2 + ch + std::to_string (count);

    //check if after compression, the length of the string reduces or not
    if (str.length() > str2.length())
        std::cout << str2 << std::endl;
    else
        std::cout << str << std::endl;
}

int main ()
{
    std::cout << "Enter a string to compress: ";
    std::string str;
    getline (std::cin, str);

    std::cout << "Compressed string is: ";
    CompressString (str);
    return 0;
}
于 2016-02-04T04:50:58.690 に答える
0

別のインプレース Java プログラムを次に示します。文字列の代わりに StringBuilder を使用できます

public static void main(String[] args) {

    String a = "aaabbccaaaddj";


        for(int i=0;i<a.length();i++){
            int c=i+1;
            int duplicateCharCount=1;
            while(c<a.length()&&a.charAt(c)==a.charAt(i)){
                ++c;
                ++duplicateCharCount;
            }

                a=a.substring(0,i+1)+duplicateCharCount+a.substring(i+duplicateCharCount);
                i++;


        }
        System.out.println(a);
    }
于 2016-10-21T14:15:18.860 に答える
0
public static String compress(String str) {
    StringBuilder result = new StringBuilder();
    int i = 0;
    int count = 0;
    while(i < str.length() - 1) {
        count++;
        if (str.charAt(i) != str.charAt(i + 1)) {
            result.append(str.charAt(i)).append(count);
            count = 0;
        }
        i++;
    }
    result.append(str.charAt(i)).append(count + 1);
    return result.toString();
}
于 2013-10-11T05:08:38.997 に答える
0
public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.print("enter the string");
    String s=(new Scanner(System.in)).nextLine();
    String s2=new String("");
    int count=0;

    for(int i=0;i<s.length();i++)
    {
        count=1;



        s2=s2+(s.charAt(i));

        while(i+1<s.length() && s.charAt(i+1)==s.charAt(i)  )
        {
            count++;

            i++;

        }

        s2=s2.concat(count+"");

        }

        System.out.print(s2);
    }

}
于 2015-02-06T08:14:06.330 に答える
0
#include<stdio.h>
#include<conio.h>

char* compress(char* str);

int main(){
  clrscr();
  char str[1000];
  scanf("%[^\n]s", str);
  char* s = compress(str);
  printf("\n%s", s);
  getch();
  return 0;
}

char* compress(char* str){
 char* s = str;
 int count = 1;
 char str2[1000] = "\0";
 char* n = str2;
 while(*(s) != '\0'){
    if(count == 1){
      *n = *s;
      n++;
    }
    if(*(s) == *(s+1)){
      count++;
      s++;
    }
    else{
     *n = '0' + count;
     n++;
     count = 1;
     s++;
    }
}
 return str2;
}
于 2019-10-11T12:04:48.570 に答える