5
if (namelist==NULL)
{
   namelist=(char**)malloc(sizeof(char**));
   namelist[i]=name;
}
else
{
   namelist=(char**)realloc(namelist,(i+1)*sizeof(char**));
   namelist[i]=name;
}

for(i=0;i<count;i++)
{
   printf("%s\t\t%s\n",namelist[i],namelist[i]);
}

問題は、入力"abcdefg"する"abcdefgh""abc"、入力として取得されることです

abcdefg          abcdefg
abcdefgh                    abcdefgh
abc              abc

2 番目と 2 番目と"abcdefgh"2 番目を一致させる方法はありますか?"abcdefg""abc"

4

3 に答える 3

8

TAB 文字 (\t) を区切り記号として使用するのをやめ、代わりに適切な書式仕様を使用します。文字列を 20 桁目から開始する場合は、次のように記述します。

printf( "%-20s%s", namelist[i],namelist[i]);
于 2012-11-23T13:05:56.550 に答える
2

これを試して:

printf("%-20s%-20s\n",namelist[i],namelist[i]);

の詳細については、こちらを参照してくださいprintf

于 2012-11-23T13:06:24.213 に答える
0

柔軟性を保つために、パラメータ化された幅を使用したい場合があります。

#include <stdio.h>

typedef enum Alignment_e
{
  alignmentUndefined = -1,
  alignmentLeft = 0,
  alignmentRight,
  alignmentMax
} Alignment_t;

typedef struct Tab_s
{
  int position;
  Alignment_t alignment;
} Tab_t;

#define FORMAT_TABBED_STRING "%*s"
#define TABBED_STRING( \
  str, \
  tab \
) \
  ((tab).alignment != alignmentLeft) ?(tab).position :-(tab).position, (str)


/* test */
int main()
{
  const Tab_t tabs[] = {
    {9, alignmentLeft},
    {32, alignmentRight},
    {-1, alignmentUndefined} /* array termination; not to be used as tab descriptor */
  }; 

  char s1[] = "s1";
  char s2[] = "s2";

  printf(FORMAT_TABBED_STRING"|"FORMAT_TABBED_STRING"|\n", TABBED_STRING(s1, tabs[0]), TABBED_STRING(s2, tabs[1]));
  printf(FORMAT_TABBED_STRING""FORMAT_TABBED_STRING"\n", TABBED_STRING(s1, tabs[1]), TABBED_STRING(s2, tabs[0]));  
}
于 2012-11-23T13:35:34.243 に答える