如何将字符串向右移动并插入空格?

时间:2018-03-11 14:05:14

标签: c string

不再需要答案 ,当然,您仍然可以发布解决方案。

我试图创建一个Farfallino Alphabet编码器,但我不知道如何将字符串向右移动并插入空格,以允许在字符串中插入新的字符。

这是我的代码:

void farfallinoEncoder(char *str){
  int i,j, lenght=strlen(str);
  char fT[4];

  for(i=0; i<lenght;i++){

    switch(str[i]){
        case 'a':
            strcpy(fT,"afa");
            break;
        case 'A':
            strcpy(fT,"Afa");
            break;
        case 'e':
            strcpy(fT,"efe");
            break;
        case 'E':
            strcpy(fT,"Efe");
            break;
        case 'i':
            strcpy(fT,"ifi");
            break;
        case 'I':
            strcpy(fT,"Ifi");
            break;
        case 'o':
            strcpy(fT,"ofo");
            break;
        case 'O':
            strcpy(fT,"Ofo");
            break;
        case 'u':
            strcpy(fT,"ufu");
            break;
        case 'U':
            strcpy(fT,"Ufu");
            break;
        default:
            printf("\n\tNope!\n"); //This message is for debug purpose only
            continue;
    }

    printf("\n\tOk!\n"); //This message is for debug purpose only

    shiftRight(str, i);

    //I'm going to add the new chars here
    for (j=0;j<strlen(fT);j++) {
        str[i+j]=fT[j];
    }
    i+=3;

    lenght=strlen(str);
  }
}

我需要将字符串向右移动并插入空格以便能够替换字符。 这是我的shiftRight功能:

void shiftRight(char *str, int pos){
  int i;

  char temp=str[pos], temp1;
  for (i=pos;i<strlen(str); i++) {
      temp1 = str[i];
      str[i] = temp;
      temp = temp1;
  }
}

但它并没有做我想做的事情!我想为新角色创造空间,但我不知道如何。现在我只是覆盖旧的。

例如,如果输入是&#34; Ciao&#34;,输出必须是&#34; Cifiafaofo&#34;,而是该功能给了我&#34; Cifi&#34;仅

我不能使用动态内存分配,因为它们还没有在我的课堂上教过。

3 个答案:

答案 0 :(得分:0)

从字符串的结尾开始会更有效,因为这会降低从O(n ^ 2)到O(n)的复杂性。假设str足够大以容纳其他字符(例如在char buf[128] = "Ciao";之类的调用者中声明它),您可以像以下一样实现编码器:

void convert(char str[])
{
    typedef char        repl_t[3];
    static repl_t const MAP[] = {
        ['a'] = "afa",
        ['A'] = "Afa",
        ['e'] = "efe",
        ['E'] = "Efe",
        ['i'] = "ifi",
        ['I'] = "Ifi",
        ['o'] = "ofo",
        ['O'] = "Ofo",
        ['u'] = "ufu",
        ['U'] = "Ufu",
    };
    static size_t const     MAP_LEN = sizeof MAP / sizeof MAP[0];

    size_t len = strlen(str);
    char *out = str + len * sizeof MAP[0];

    *out = '\0';
    while (len > 0) {
        size_t  c = str[len - 1];

        if (c >= MAP_LEN || MAP[c][0] == '\0') {
            out -= 1;
            *out = c;
        } else {
            out -= sizeof MAP[c];
            memcpy(out, MAP[c], sizeof MAP[c]);
        }

        --len;
    }

    memmove(str, out, strlen(out) + 1);
}

答案 1 :(得分:0)

感谢@AndersK,我找到了这个解决方案。

void farfallinoEncoder(char *str, char *translated){
  int i,l=0;

  for (i=0;i<strlen(str);i++){
      switch( tolower((unsigned char)str[i]) ){
          case 'a':
          case 'e':
          case 'i':
          case 'o':
          case 'u':
              translated[l]=str[i];

              if( isupper((unsigned char)str[i]) )
                  translated[l+1]='F';
              else
                  translated[l+1]='f';

              translated[l+2]=str[i];

              l+=3;
              break;
         default:
              translated[l]=str[i];
              l++;
              break;
      }
  }

  translated[l]='\0';
}

我应该停止使我的生活变得无用,感谢所有人的耐心和合作。如果您找到任何其他解决方案,请发布它,我会看看它! ; d

答案 2 :(得分:0)

足够使用2次传球:一次找到新长度,第二次做移动。

实际代码通常需要检查数组大小以防止缓冲区溢出,

#include <string.h>
#include <stdlib.h>

const char *farfallino_haystack = "AEIOUaeiou";

size_t farfallino_encoder_size(const char *str) {
  size_t sz = 1; // \0
  while (*str) {
    int needle = *str;
    // look for a needle in a hay stack
    if (strchr(farfallino_haystack, needle)) {
      sz += 3;
    } else {
      sz += 1;
    }
    str++;
  }
  return sz;
}

int farfallino_encode_inplace(char *str, size_t sz) {
  size_t sz_needed = farfallino_encoder_size(str);
  if (sz_needed > sz) {
    return -1; // let calling code handle this
  }
  size_t len_needed = sz_needed - 1;
  size_t len_was = strlen(str);
  str[len_needed] = '\0';

  // Starting as the end and working to the "left"
  // while some encoding work still needed
  while (len_needed > len_was) {
    int needle = str[--len_was];
    if (strchr(farfallino_haystack, needle)) {
      str[--len_needed] = tolower(needle);
      str[--len_needed] = 'f';
      str[--len_needed] = needle;
    } else {
      str[--len_needed] = needle;
    }
  }
  return 0;
}

测试

void farfallino_encode_test(char *str, size_t sz) {
  size_t n = farfallino_encoder_size(str);
  printf("%zu <%s> --> ", sz, str);
  if (farfallino_encode_inplace(str, sz)) {
    puts("too small");
  } else {
    printf("%zu <%s>\n", n, str);
  }
}

int main(void) {
  char buf[11];
  strcpy(buf, "Ciao");
  farfallino_encode_test(buf, sizeof buf);
  strcpy(buf, "ABCDE");
  farfallino_encode_test(buf, sizeof buf);
  strcpy(buf, "abcdefghi");
  farfallino_encode_test(buf, sizeof buf);
}

输出

11 <Ciao> --> 11 <Cifiafaofo>
11 <ABCDE> --> 10 <AfaBCDEfe>
11 <abcdefghi> --> too small