当前位置:首页 » 编程语言 » c语言怎么转换成数组
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言怎么转换成数组

发布时间: 2022-08-20 10:15:29

1. c语言 怎么把字符串转换成数组

字符串输出在遇到'\0'字符前不会停
char*
p
=
"123\0456";
printf(p);
//
输出123
p[3]
=
'&';
printf(p);
//
输出123&456
你代码里为什么那样是因为多位数组的存储空间是连续的
两句strcpy后arr数组是变这样
you.me.?
其中.代表空字符,?是未使用的空间

2. C语言整数转数组

#include<stdio.h>
int main(){
int n=123456;
int a[20];//储存数位
int k=0;//记录数位长度
while(n!=0){
a[k++]=n%10;//提取n的各个数位上的数
n/=10;
}
for(int i=k-1;i>=0;i--)//逆序输出
printf("%d ",a[i]);
return 0;
}

3. C语言一个二维数组如何转换成一个新的一维数组

当然可以了。

例如:


#include<stdio.h>voidmain(){inti,j,k=0,a[2][2]={1,2,3,4},b[4];for(i=0;i<2;i++)for(j=0;j<2;j++){b[k]=a[i][j];k++;}for(k=0;k<4;k++)printf("%d",b[k]);}

运行结果:

就是将一个2×2的二维数组的值按顺序赋给一个一维数组。

4. c语言怎样输入字符串后转换为数组

#include
<stdio.h>
#include
<string.h>//为strlen()函数提供原型
int
main()
{
char
ch[100];
int
i,length;
int
n_number=0,m_number=0;
printf("请输入字符串:
");
gets(ch);//获取输入,并存入数组中
length=strlen(ch);//计算输入字符串的长度
for(i=0;i<length;i++)
{
if(ch[i]>='0'&&ch[i]<='9')//判断是否为数字
{
n_number++;
}
if(ch[i]>='a'&&ch[i]<='z'||ch[i]>='A'&&ch[i]<='Z')//判断是否为字母
{
m_number++;
}
}
printf("输入的字符串为:
");
puts(ch);//输出存入数组的字符串
printf("字符串中数字的个数=%d,字母的个数=%d\n",n_number,m_number);
return
0;
}

5. C语言怎么把一个文件转换为数组

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

struct stuInfo{
char name[20];
int num;
int a,b,c,d;
};

int main(){
FILE *fp;
char fileName[]="a.txt";
int n,i;
struct stuInfo stu[1000];
if((fp=fopen(fileName,"r"))==NULL){
printf("Error!\n");
exit(0);
}
fscanf(fp,"%d",&n);
for(i=0;i<n;i++){
fscanf(fp,"%d%s%d%d%d%d",&stu[i].num,
&stu[i].name,
&stu[i].a,
&stu[i].b,
&stu[i].c,
&stu[i].d
);
printf("%d %s %d %d %d %d\n",stu[i].num,
stu[i].name,
stu[i].a,
stu[i].b,
stu[i].c,
stu[i].d
);
}
fclose(fp);
return 0;

6. [C语言]怎么把一段输入转换为数组

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

int main ()
{
int n, i;
int *a;
scanf ("%d", &n); /*读入n*/
a = (int*) malloc (sizeof(int)*n); /*申请n整数的空间*/
for (i = 0; i < n; i ++) /*依次读入每一个元素*/
scanf ("%d", &a[i]);
.
. /*其他代码,比如对a的引用之类的*/
.
.
free (a); /*最后要释放空间*/

}

7. 用C语言进行字符串到数组的转化

#include <stdio.h>
#include <string.h>//为strlen()函数提抄供原型
int main()
{ char ch[100];
int i,length;
int n_number=0,m_number=0;
printf("请输入字符串: ");
gets(ch);//获取输入,并存入数组中
length=strlen(ch);//计算输入字符串的长度
for(i=0;i<length;i++)
{
if(ch[i]>='0'&&ch[i]<='9')//判断是否为数字
{
n_number++;
}
if(ch[i]>='a'&&ch[i]<='z'||ch[i]>='A'&&ch[i]<='Z')//判断是否为字母
{
m_number++;
}
}
printf("输入的字符串为: ");
puts(ch);//输出存入数组的字符串
printf("字符串中数字的个数=%d,字母的个数=%d\n",n_number,m_number);
return 0;
}

8. c语言如何将浮点型数据转换为数组

库函数gcvt可以完成此任务,它返回转换后的字符串的地址。如:
//#include
"stdafx.h"//If
the
vc++6.0,
with
this
line.
#include "stdio.h"
#include "stdlib.h"
int main(void){
double x=34.743829109;
char a[19];
printf("%s\n",gcvt(x,5,a));//5确定有效数字长度且据其后数字四舍五入
return 0;
}

9. 用c语言写代码三个一维数组如何转换成一个二维数组

int a[10],b[10],c[10];
int d[10][3];
for(int i=0;i<10;i++)
d[0][i] = a[i];
for(int i=0;i<10;i++)
d[1][i] = b[i];
for(int i=0;i<10;i++)
d[2][i] = c[i];