在C中反转二维字符数组

时间:2015-06-28 09:15:00

标签: c string

我正在尝试编写一个程序来反转整个字符串,并且还可以打印多次反转字符串中的每个单词。 例如我的字符串是: “2 狐狸跳过懒狗。“ 为此,输出应为: .god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF。 我试图将每个单词存储在一个二维数组中然后反转每个单词,但我无法做到这一点。 这是我的代码。善意的帮助 注意:我们如何在控制台中提供EOF

#include <stdio.h>
#include <stdlib.h>

int main() {
    char string[100][100];
    char ch;
    int i = 0, j = 0, l = 0, count = 0, x = 0, y = 0;
    while ((ch = getchar()) != EOF) {
        string[i][l++] = ch;
        if (ch == ' ') {
            string[i][l] = '\n';
            i++;
            l = 0;
            count++;
        }
    }
    for (x = 0; x <= count; x++) {
        int length = strlen(string[x]) - 1;
        for (y = length; y >= 0; --y)
            printf("%s", string[y]);
    }
    return 0;
}

4 个答案:

答案 0 :(得分:2)

以下是您的代码的一些更改,请检查注释以获取解释。

int main()
{    
char string[100][100];
char ch;
int i=0,j=0, l=0, count=0, x=0, y=0;
while((ch=getchar())!=EOF)
{
    string[i][l++] = ch;
    if(ch==' ')
     {
       string[i][l] = '\0';    //make the string null terminated otherwise you cant work with its length
       i++;
       l=0;
       count++;
    }
}
string[i][l]='\0';    //make the last string null terminated
for(x=count; x>=0; x--)        //read from last counter 
{
   int length = strlen(string[x])-1;
   for(y=length; y>=0; --y)
   {
      printf("%c", string[x][y]);       //print by each character and not string.
   }
}
return 0;

}

答案 1 :(得分:2)

代码中的更正:

  1. C字符串以空值终止,但您使用换行符\n字符终止字符串,这是错误的。
  2. 您正在使用字符串存储空白,在这种情况下反转将很困难。
  3. 您的print语句不会反转字符串,逐字符打印。
  4. 对于您需要的输出,您可以考虑代码

    #include <stdio.h>
    #include <stdlib.h>
    
    int main() {
        char string[100][100];
        char ch;
        int i = 0, j = 0, l = 0, count = 0, x = 0, y = 0;
        while ((ch = getchar()) != EOF) {
            if (ch == ' ') {
                string[i][l] = '\0';    /// Null terminate the string
                i++;
                l = 0;
                count++;
            }
            else
                string[i][l++] = ch;   /// Don't add whitespace to the string
        }
        string[i][l] = '\0';
        for (x = count; x >= 0; x--) {
            int length = strlen(string[x]) - 1;
            for (y = length; y >= 0; --y)
                printf("%c", string[x][y]);   /// Print the string in reverse
            printf(" ");
            for (y = length; y >= 0; --y)
                printf("%c", string[x][y]);   /// Twice
            printf(" ");
        }
        return 0;
    }
    

    <强>输入

    2 Fox jumps over the lazy dog.
    

    <强>输出

    .god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF 2 2 
    

    请参阅http://ideone.com/qaIoW9

答案 2 :(得分:2)

如果我理解你想复制字符串中第一个数字所指定的每个反转字N次,那么下面的内容就可以了:

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

char *strrevdup (char* str);

int main (int argc, char **argv) {

    if (argc < 2 ) {
        fprintf (stderr, "error: insufficient input, usage: %s \"# string\"\n", 
                argv[0]);
        return 1;
    }

    char *str = strdup (argv[1]);
    char *p = str;
    char *rev = NULL;
    int mult = 0;
    int i = 0;

    while (*p && *p != ' ') p++;
    *p = 0;
    mult = atoi (str);
    *p = ' ';
    if (!mult) return 1;
    while (*p && *p == ' ') p++;

    rev = strrevdup (p);
    char *r = rev;

    printf ("\n the reversed string with duplicated words '%d' times is:\n\n", mult);
    for (p = strtok (r, " "); p; p = strtok (NULL, " \n"))
        for (i = 0; i < mult; i++)
            printf (" %s", p);
    printf ("\n\n");

    free (str);
    free (rev);

    return 0;
}

/** strrevdup - reverse duplicate string, swaps src & dest each iteration.
*  Takes valid string, duplicates and reverses, original is preserved.
*  Returns pointer to reversed string on success, NULL otherwise.
*  Requires string.h, caller is responsible for freeing memory allocated.
*/
char *strrevdup (char* str)
{
    if (!str) {
        printf ("%s() error: invalid string\n", __func__);
        return NULL;
    }

    char *rstr = strdup (str);
    char *begin = rstr;
    char *end = rstr + strlen (rstr) - 1;
    char tmp;

    while (end > begin){
        tmp=*end;
        *end-- = *begin;
        *begin++ = tmp;
    }

    return rstr;
}

<强>输出

$ ./bin/strrevndup "2 Fox jumps over the lazy dog."

 the reversed string with duplicated words '2' times is:

 .god .god yzal yzal eht eht revo revo spmuj spmuj xoF xoF

答案 3 :(得分:0)

您可以尝试使用此代码,但它会反转并打印不同行中的单词。您可以尝试更多的内容来获得所需的答案。

` #include <stdio.h>
  #include <stdlib.h>
  #include<string.h>
  int main()
  {
   char string[1024][1024];
   char ch;
   int t,z;
   scanf("%d",&t);
   z=t;
   int i=0, l=0, count=0, x=0, y=0;
   getchar();
   while((ch=getchar())!=EOF)
    {
     if(ch=='\n')
      {
        i++;
        l=0;
        count++;
        string[i][l++] = ch;
        i++;
        l=0;
      }
     else if(ch==' ')
     {
       i++;
       l=0;
       count++;
     }
     else{
        string[i][l++] = ch;
         }
  }
  for(x=count+1; x>=0; x--)
   {
     if(string[x][0]=='\n')
     {
       printf("\n");
     } 
   else{
        char *rev=strrev(string[x]);
        while(t--)
        printf("%s ",rev);
        t=z;
        }
     }
     return 0;
}`