473,785 Members | 2,317 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1743
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.l earn.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
2465
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 nightmare. There are of course many solutions, but they all end up forcing you to abandon the array syntax in favour of macros or functions. Now I have two questions - one is historical, and the other practical. 1.) Surely malloc (and...
4
1823
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 data-type) in a SQL DB need to be written to an ASP.NET page. The BLOB is actually wrapped by a class written in C#, BlobObj, which can return a byte-array containing the image-bytes. I can successfully write the image to the page using the...
1
1210
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 <qsales> Quarterly Sales. (and includes children) If I could use an array syntax, it would be: <qsales> <qsales>
12
112102
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
2050
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
8135
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> #include<stdlib.h> #include<math.h>
4
10516
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 doesn't seem to write to the screen in the way I would expect. The output is:
17
3381
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 programme: printf("\nCommand line arguement %d: %s.", i , argv ); The porgramme outputs 3 of the command line arguements, then gives a segmentation fault on the next line, followed by other strange
3
3243
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 flashcards.txt and then gets the categories from there. Anyhow, I understand how to do this, and there is nothing wrong with my syntax, because it compiles properly, but whenever it runs, it just starts outputting a bunch of random text, although I did...
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9946
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8968
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.