我的格式爲給定長度的所有字符串列表Ç - 獲取字符串從一個int
如長度3
000
100
200
.
.
900
A00
B00
.
.
Z00
a00
b00
.
.
z00
010
.
.
.
zzz
我想寫valueof(int pos, int len)
它接受一個位置INT作爲參數,並在該位置(例如valueof(1,3)
打印000
和valueof(109,3)
打印k10
打印字符串這是我的嘗試,它是不工作:
void valueof(int pos,int len)
{
int i=0;
char arry[62] = {'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char * string = malloc(sizeof(char)*(len+1));
for(i=0; i<len; i++)
{
string[i]='0';
}
string[len]='\0';
i=0;
while(pos >= 62)
{
string[i]=arry[(pos%62)-1];
i++;
pos/=62;
}
string[i]=arry[pos];
printf("%s\n",string);
}
它是如何不工作?例如,如果我們調用'valueOf(109,3)'當前報告的內容? – 2014-10-01 17:02:40
閱讀關於[snprintf(3)](http://man7.org/linux/man-pages/man3/snprintf.3.html)&[strdup(3)](http://man7.org/linux/ man-pages/man3/strdup.3.html)或許(在Linux上)[asprintf(3)](http://man7.org/linux/man-pages/man3/asprintf.3.html) – 2014-10-01 17:11:14
@BasileStarynkevitch :我不確定這有幫助。 – 2014-10-01 17:12:05