By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,498 Members | 1,783 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,498 IT Pros & Developers. It's quick & easy.

Outputting the size of an array

P: n/a
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;
}

Nov 15 '05 #1
Share this Question
Share on Google+
20 Replies


P: n/a
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.

Nov 15 '05 #2

P: n/a
tigrfire wrote:
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;
}


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],

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

Nov 15 '05 #3

P: n/a
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

Nov 15 '05 #4

P: n/a

tigrfire wrote:
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


int main()
{
inputPhrase();

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

return 0;
}

You are calling "inputPhrase();" twice ! . remove it from the line
after main(){. It works fine for me.

Nov 15 '05 #5

P: n/a
You are not setting `n' to the number of characters input, this might
help.

/*CODE BEGINS*/

int inputPhrase()
{
char msg[characters], ch;
int i = 0;
int n = 0;
while ((ch = getchar()) != '\n')
{
msg[i++] = ch;
}
msg[i] = '\0'; /* prevents garbage from being place at end of array
output */
/*****/
n = i;
/*****/
i = 0;
while (msg[i] != '\0')
{
printf("%c", msg[i++]);
}
return n;
}

int main()
{
int size = inputPhrase();
printf("%d\n", size);
return 0;
}
/*CODE ENDS*/

and remove that extra call to inputPhrase from your main.

HTH.

Nov 15 '05 #6

P: n/a
On 14 Nov 2005 18:34:00 -0800, "tigrfire" <bb******@gmail.com> wrote
in comp.lang.c:
Outside the function I defined characters as:
#define characters 30


Then you should follow the very commonly used convention and define
macros in ALL UPPER CASE.

#define CHARACTERS 30

That way, you would have posted:

char msg[CHARACTERS], msg;

....and most people would have assumed it was a macro.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #7

P: n/a
Sandeep wrote:
tigrfire wrote:
[...]
Further ,
char msg[characters],


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

That's not true, C99 has VLAs (Variable Length Arrays) where in you can
use a variable to initialize the array size.
Nov 15 '05 #8

P: n/a

That's not true, C99 has VLAs (Variable Length Arrays) where in you can
use a variable to initialize the array size.


I stand corrected. I tried it and it is working.

Nov 15 '05 #9

P: n/a
I've modified my code as you mentioned and also made some syntatical
changes that Jack recommended. I'm now trying to do a number of things
with my code:
1. I'm writing a function called cleanUpPhrase that removes
punctuation and whitespace. I'm going to try and accomplish this using
tolower() and isAlpha() from the ctype library I've included. I'm just
a little confused on how I can pass the string of chars from my
inputPhrase function into my cleanUpPhrase function.
2. On another note, I'm also trying to modify my inputPhrase
function so that if the user-inputted string of chars is longer than 30
chars long, it will truncate the message and only output the first 30
chars. In my main function I used an if loop to truncate the size, but
nothing I've tried in my inputPhrase function to truncate the actual
returned message output is working.

Here's the code:
// code starts

#include <stdio.h> /* provides scanf, printf, getchar */
#include <ctype.h> /* provides tolower, isalpha */

#define MAX_CHARS 30 /* defines the value of characters equal to 30 as
a global
constant */

int inputPhrase(); /* This function reads in a phrase, maximum of 30
characters,
entered by the user. Function returns the phrase, stored in
a character array. Also returns the size of the array. Only
the first 30 characters are stored in the array if the array
size is greater than 30. */

int inputPhrase()
{
char msg[MAX_CHARS], ch;
int i = 0;
int n = 0;
while((ch = getchar()) != '\n')
{
msg[i++] = ch;
}
msg[i] = '\0'; /* prevents garbage from being place at end of array
output */

n = i;

i = 0;

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

}

void cleanUpPhrase()
{

}

int main()
{
int size = inputPhrase();

if(size >= MAX_CHARS)
{
size = 30;
}

printf("\n%d\n", size);
return 0;
}

//code ends

Nov 15 '05 #10

P: n/a
tigrfire wrote:

I've modified my code as you mentioned and also made some syntatical
changes that Jack recommended. I'm now trying to do a number of things
with my code:
1. I'm writing a function called cleanUpPhrase that removes
punctuation and whitespace. I'm going to try and accomplish this using
tolower() and isAlpha() from the ctype library I've included. I'm just
a little confused on how I can pass the string of chars from my
inputPhrase function into my cleanUpPhrase function.
2. On another note, I'm also trying to modify my inputPhrase
function so that if the user-inputted
string of chars is longer than 30
chars long, it will truncate the message and only output the first 30
chars. In my main function I used an if loop to truncate the size, but
nothing I've tried in my inputPhrase function to truncate the actual
returned message output is working.

Here's the code:


/* BEGIN new.c */

#include <stdio.h>
#include <ctype.h>

#define MAX_CHARS 30

size_t inputPhrase(char *msg, size_t max);

int main(void)
{
char msg[MAX_CHARS + 1];
size_t size;

size = inputPhrase(msg, sizeof msg - 1);
printf("\n%u\n", (unsigned)size);
puts(msg);
return 0;
}

size_t inputPhrase(char *msg, size_t max)
{
size_t i;
int ch;

ch = getchar();
for (i = 0; max > i && ch != '\n' && ch != EOF; ++i) {
msg[i] = (char)ch;
ch = getchar();
}
msg[i] = '\0';
return i;
}

void cleanUpPhrase()
{

}

/* END new.c */
--
pete
Nov 15 '05 #11

P: n/a
On 14 Nov 2005 18:34:00 -0800, in comp.lang.c , "tigrfire"
<bb******@gmail.com> wrote:
Outside the function I defined characters as:
#define characters 30


(and other contextless remarks)

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 15 '05 #12

P: n/a
tigrfire wrote:

I've modified my code as you mentioned and also made some syntatical
changes that Jack recommended. I'm now trying to do a number of things
with my code:
1. I'm writing a function called cleanUpPhrase that removes
punctuation and whitespace. I'm going to try and accomplish this using
tolower() and isAlpha() from the ctype library I've included.


There's also ispunct() and isspace() to consider.

--
pete
Nov 16 '05 #13

P: n/a
In article <ec********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
On 14 Nov 2005 18:34:00 -0800, in comp.lang.c , "tigrfire"
<bb******@gmail.com> wrote:
Outside the function I defined characters as:
#define characters 30


(and other contextless remarks)

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.


You (and others, such as Keith) are wasting your breath. They'll never get
it. And I'll tell you why.

Imagine that there's a mouse - and the mouse is the Usenet. You and I can
see that it is a mouse and we behave accordingly. But now there is a class
of users (we'll call them "googlers") that are wearing these funny weird
glasses that make them see not a mouse, but an elephant. Seeing an
elephant (i.e., the Usenet as a web page), they also behave accordingly.
And no amount of verbiage from us is going to convince them that it's not
an elephant - that it is only a mouse.

To make this more clear, to a googler, it doesn't make any sense to "quote"
(whatever the heck that is...), in fact, to do so would be absurd, when all
the rest of the articles in the thread are right there in front of their
faces (just as clear as the trunk on that mouse, er, elephant). And no
amount of verbiage from us is going to convince them not to believe what
they see. The point is you can *never* convince someone that what they see
isn't reality. The only way you can address the problem is to help them
fix their eyesight (or help them remove their funny glasses).

Or, just let it go...

Nov 16 '05 #14

P: n/a
ga*****@yin.interaccess.com (Kenny McCormack) writes:
In article <ec********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
On 14 Nov 2005 18:34:00 -0800, in comp.lang.c , "tigrfire"
<bb******@gmail.com> wrote:
Outside the function I defined characters as:
#define characters 30


(and other contextless remarks)

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.


You (and others, such as Keith) are wasting your breath. They'll never get
it. And I'll tell you why.


You've told us why several times before. We've explained why you're
wrong (briefly, many of them *do* get it). Save your own breath.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 16 '05 #15

P: n/a
On Wed, 16 Nov 2005 21:15:55 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:
(the usual nonsense)
You've told us why several times before. We've explained why you're
wrong (briefly, many of them *do* get it). Save your own breath.


I don't see why you don't killfile the idiot. He rarely says anything
worth listening to, and is erroneous insufficiently often to endure
just so you can correct him.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 17 '05 #16

P: n/a
On Wed, 16 Nov 2005 21:15:55 GMT, in comp.lang.c , Kenny McCormack
wrote:
You (and others, such as Keith) are wasting your breath.

I dont use my breath to power my computer. I don't emit that sort of
quantity of hot air....

(sorry, the threading may get hosed on this, Kenny's in my killfile so
I had to reply to Keith and update the attrib line).
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 17 '05 #17

P: n/a
In article <ln************@nuthaus.mib.org>,
Keith Thompson <ks***@mib.org> wrote:
....
You've told us why several times before. We've explained why you're
wrong (briefly, many of them *do* get it). Save your own breath.


Au contraire. 'tis you that is wrong.

And that is why I must keep posting. To set the record straight each time.

Nov 17 '05 #18

P: n/a
Kenny McCormack wrote:

Au contraire. 'tis you that is wrong.

And that is why I must keep posting. To set the record straight each time.


Actually, people get it as long as there's someone to correct them
often. Google made it
easier for people to get on nntp groups but many, even people who posted
before using
an Usenet client and switched to Google groups don't realize that the
Reply link doesn't
work properly, and that it doesn't hide but it totally annihilates the
original
message. Also, warning people about top posting is a good idea not
necessarily for the sake
of the guys who post but for the people who bother to read. People
coming to a newsgroup
looking for an answer to a question should be aware that if someone
bothers to read
they should also use common sense when replying.

This discussion will degenerate into a children's fight... someone wants
to have the last word
even if he's not right. If you have the will to post to fight something
that makes no sense, why not
So why not let people warn others if they are willing to do that?
I hope I won the war and have the last post on this :-).
Nov 17 '05 #19

P: n/a
>many, even people who posted
before using
an Usenet client and switched to Google groups don't realize that the
Reply link doesn't
work properly, and that it doesn't hide but it totally annihilates the
original message.
just for sake of information, the reply link at bottom is quick reply
don't use it. instead near the name of poster, on top of message, click
options, then reply...
Also, warning people about top posting is a good idea not
necessarily for the sake of the guys who post but for the people who bother to read.
That's really a good idea...
People coming to a newsgroup
looking for an answer to a question should be aware that if someone
bothers to read
they should also use common sense when replying.
alas! it's not that common :P.
This discussion will degenerate into a children's fight... someone wants
to have the last word even if he's not right.
:D
If you have the will to post to fight something that makes no sense, why not
So why not let people warn others if they are willing to do that?
I hope I won the war and have the last post on this :-).


let's see.

Nov 17 '05 #20

P: n/a
In article <j8********************************@4ax.com>,
Mark McIntyre <ma**********@spamcop.net> wrote:
On Wed, 16 Nov 2005 21:15:55 GMT, in comp.lang.c , Keith Thompson
<ks***@mib.org> wrote:
ga*****@yin.interaccess.com (Kenny McCormack) writes:


(the usual nonsense)

You've told us why several times before. We've explained why you're
wrong (briefly, many of them *do* get it). Save your own breath.


I don't see why you don't killfile the idiot. He rarely says anything
worth listening to, and is erroneous insufficiently often to endure
just so you can correct him.


I see what you're saying, but, hey, call me a masochist, but I find Keith
amusing.

Nov 23 '05 #21

This discussion thread is closed

Replies have been disabled for this discussion.