473,326 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Outputting the size of an array

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
20 1693
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
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
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

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

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
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
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
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
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
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
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
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
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
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
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
>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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
4
by: Jonathan | last post by:
I have a client solution that requires data and associated files to be stored with data in a database. As such, I have a situation where JPEG thumbnails/images that are stored as BLOBs (image...
1
by: awebguynow | last post by:
I know its redundant but thats what this report calls for. For every node except the first, I want to output a pair of nodes: the current and prev one. For sake of example, lets call the node...
12
by: manochavishal | last post by:
Hi, I have a question. How can i know the size of array when it is passed to a function. For Example i have this code: #include <stdio.h> #include <stdlib.h>
4
by: superja | last post by:
I have some problem outputting double variable types. Below is my script: #include <stdio.h> #include <math.h> double x; double h;
7
by: bowlderster | last post by:
Hello,all. I want to get the array size in a function, and the array is an argument of the function. I try the following code. /*************************************** */ #include<stdio.h>...
4
by: Peter Nimmo | last post by:
Hi, I am writting a windows application that I want to be able to act as if it where a Console application in certain circumstances, such as error logging. Whilst I have nearly got it, it...
17
by: Matt | last post by:
Hello. I'm having a very strange problem that I would like ot check with you guys. Basically whenever I insert the following line into my programme to output the arguments being passed to the...
3
by: mohaakilla51 | last post by:
Alright guys, I am working on a flashcard script... Previously I had it so that it onlty had predefined categories. People were complaining, so now I am trying to make it to where it reads...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.