输出数组的大小 [英] Outputting the size of an array

查看:116
本文介绍了输出数组的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下代码来返回存储在

数组中的字符串,并返回数组的大小。我想在没有

的情况下使用指针进行操作,到目前为止,该字符存储工作正常,它只是我不能想到一种方式返回数组中

个字符的数量。有人帮帮我吗?


int inputPhrase()

{

char msg [characters],ch;

int i = 0;

int n = 5;


while((ch = getchar())!=''\ n '')

{

msg [i ++] = ch;

}


msg [我] =''\'''; / *防止垃圾放在阵列的末尾

输出* /


i = 0;


而(msg [i]!=''\ 0'')

{

printf("%c",msg [i ++]);

}


返回n;

}


int main()

{

inputPhrase();


int size = inputPhrase();

printf("%d \\ \\ n \\ n,尺寸);


返回0;

}

I need the following code to return a string of character stored in
array and also return the size of the array. I want to do it without
using pointers and so far have the character storing working fine, it''s
just that I can''t really think of a way to return the number of
characters in the array. Anyone help me out?

int inputPhrase()
{
char msg[characters], ch;
int i = 0;
int n = 5;

while ((ch = getchar()) != ''\n'')
{
msg[i++] = ch;
}

msg[i] = ''\0''; /* prevents garbage from being place at end of array
output */

i = 0;

while (msg[i] != ''\0'')
{
printf("%c", msg[i++]);
}

return n;
}

int main()
{
inputPhrase();

int size = inputPhrase();
printf("%d\n", size);

return 0;
}

推荐答案

忽略n = 5;我只是用它来确保main是正确输出n,显然,n需要最初设置为0

然后增加到适当的字符数

数组。

Ignore the n = 5; I just used that to make sure that main was
outputting n correctly, obviously, n needs to be originally set to 0
then incremented up to the appropriate number of characters in the
array.


tigrfire写道:
tigrfire wrote:
我需要以下代码才能返回一个字符串存储在
数组中的字符,也返回数组的大小。我想在没有使用指针的情况下做到这一点,到目前为止,角色存储工作正常,它只是因为我不能想到一种方法来返回
数组中的字符。有人帮帮我吗?

int inputPhrase()
{char msg [characters],ch;
int i = 0;
int n = 5;

while((ch = getchar())!=''\ n'')
{
msg [i ++] = ch;
}

msg [i] =''\'''; / *防止垃圾放在阵列末端
输出* /

i = 0;

while(msg [i]!=''\ 0'')
{
printf("%c",msg [i ++]);
}

返回n;
}

int main()
{
inputPhrase();

int size = inputPhrase();
printf("%d \ n",size);

返回0;
}
I need the following code to return a string of character stored in
array and also return the size of the array. I want to do it without
using pointers and so far have the character storing working fine, it''s
just that I can''t really think of a way to return the number of
characters in the array. Anyone help me out?

int inputPhrase()
{
char msg[characters], ch;
int i = 0;
int n = 5;

while ((ch = getchar()) != ''\n'')
{
msg[i++] = ch;
}

msg[i] = ''\0''; /* prevents garbage from being place at end of array
output */

i = 0;

while (msg[i] != ''\0'')
{
printf("%c", msg[i++]);
}

return n;
}

int main()
{
inputPhrase();

int size = inputPhrase();
printf("%d\n", size);

return 0;
}




你已初始化n,但从未使用过它!


while((ch = getchar())!=''\ n'')

{

msg [i ++] = ch;

}


n = i; //这应该设置n的值。


此外,



You have initialized n, but never used it !

while ((ch = getchar()) != ''\n'')
{
msg[i++] = ch;
}

n = i; // This should set the value of n .

Further ,

char msg [characters],
char msg[characters],



这不是一个有效的语句,无法使用

变量初始化数组。


This is not a valid statement, an array cannot be initialized with a
variable.


在函数外部我将字符定义为:

#define characters 30


如果我按照你说的去做并修改我的代码以便n =我在线上你

参考,程序的输出是这样的:


这是样本输出//输入的字符串

这是样本输出//返回字符串

0 //值n

Outside the function I defined characters as:
#define characters 30

If I do as you say and modify my code so that n = i on the line you
reference to, the program''s output is this:

this is sample output //entered string of chars
this is sample output //returned string of chars
0 //the value of n


这篇关于输出数组的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆