有什么C程序不会做。
{.对字符数组c1赋 '0'~'9',
对字符数组c2赋 'A'~'Z',然后输出c1和c2数组中的数据.}
请个高手帮个忙
有什么C程序不会做。
{.对字符数组c1赋 '0'~'9',
对字符数组c2赋 'A'~'Z',然后输出c1和c2数组中的数据.}
请个高手帮个忙
char c1[10] = "0123456789";
char c2[26] = "ABC.......XYZ";
printf("%s\n%s\n", c1, c2);
或者
char c1[10] = {0};
char c2[26] = {0};
int i = 0;
for (i = 0; i < 10; i++)
c1[i] = '0' + i;
for (i = 0; i < 26; i++)
c2[i] = 'A' + i;
printf(...);
char c1[9],c2[26];
for (i = 0; i < 10; i++)
c1[i] = '0' + i;
for (i = 0; i < 26; i++)
c2[i]='A'+i;
for (i = 0; i < 10; i++)
cout<<c1[i]<<" ";
cout<<endl;
for (i = 0; i < 26; i++)
cout<<c2[i]<<" ";
这是什么问题,
void set_array(char *A[], char char_begin, char char_end) // 假设数组A足够大,
{
while(char_begin <= char_end)
*A++ = begin++;
*A = '\0'; // make it to c character string ;
}
int main()
{
char a[50], b[50];
set_array(a, '0', '9');
set_array(b, 'A', 'Z');
printf("%s\t%s", a, b);
return 0;
}
下面写一个更简单方便的;依赖于expand(char A[], int size);
void expand(char *s)
{ char c;
char *temp = (char*)malloc(sizeof(char) * size); // to store the expanded string;
while((c = *s++) != '\0')
{
if(*s == '-' && *(s+ 1) >= c) // we need to expand
{
while(c <= *++s)
*tmep++ = c++;
}
else
*temp++ = c;
}
*temp = '\0';
strcpy(s, temp); // copy back to s;
free(temp);
}
下面可以这样写,
#include <stdio.h>
#include <string.h>
int main()
{
char a[30] = "0-9"; // not chara[30] = "0123....9";
char b[30] = "A-Z"; // not char b[30] = "ABC....Z";
expand(a, 30);
expand(b, 30);
cout << a << "\t" << b;
return 0;
}