Outputting the size of an array

T

tigrfire

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 = '\0'; /* prevents garbage from being place at end of array
output */

i = 0;

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

return n;
}

int main()
{
inputPhrase();

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

return 0;
}
 
T

tigrfire

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

Sandeep

tigrfire said:
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 = '\0'; /* prevents garbage from being place at end of array
output */

i = 0;

while (msg != '\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.
 
T

tigrfire

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
 
S

Sandeep

tigrfire said:
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.
 
H

haroon

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 = '\0'; /* prevents garbage from being place at end of array
output */
/*****/
n = i;
/*****/
i = 0;
while (msg != '\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.
 
J

Jack Klein

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

Sandeep

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

tigrfire

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 = '\0'; /* prevents garbage from being place at end of array
output */

n = i;

i = 0;

while(msg != '\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
 
P

pete

tigrfire said:
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 = (char)ch;
ch = getchar();
}
msg = '\0';
return i;
}

void cleanUpPhrase()
{

}

/* END new.c */
 
P

pete

tigrfire said:
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.
 
K

Kenny McCormack

(and other contextless remarks)

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

Keith Thompson

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

Mark McIntyre

(e-mail address removed) (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.
 
M

Mark McIntyre

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).
 
K

Kenny McCormack

Keith Thompson said:
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.
 
J

John

Kenny said:
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 :).
 
H

haroon

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,475
Latest member
NovellaSce

Latest Threads

Top