Beginner C/C++

Moderators: None (Apply to moderate this forum)
Number of threads: 5428
Number of posts: 16943

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
strings and arrays task Posted by illay1st on 25 Mar 2011 at 5:34 PM
hey everybody!

i'm really new to c programing , i truly hopes to get some ideas
( hints if possible ) to a task in c that i'v been given.

the task goes like this:

1. Input the string

Description: Input the string after the following message
Please, enter your string>
Maximal allowed string length is 420. Each number in the string must be maximum
20 digits, and there must be maximum 20 numbers. In case of empty string or string
consisting characters which are not digits or spaces, as well as in case of more than
20 numbers in the string, or in case of number longer than 20 digits, the following
message will appear:
ERROR: Bad string, try again.
and the program asks to input the string again.

these are the instructions , i don't know how what is an empty string , or how should i deal with it, and what kind of variables i should use ( i'm not allowed to use string function , or any other kind of funcitons,only strings and arrays...

thank you all!
Report
Re: strings and arrays task Posted by pseudocoder on 25 Mar 2011 at 11:07 PM
declare a char array
char arr[420];

input a string
fgets(arr, sizeof arr, stdin);

if it's empty
if(arr[0] == 0)

if it's greater than 20
if(strlen(arr) > 20)

non-digits/spaces may take some work
char flag = 0,
     s[] = "123 some text 456";
int i;

for(i=0; s[i] != '\0'; ++i) {
   if(!isdigit(s[i]) && !isspace(s[i])) {
      flag = 1; break;
   }
}

if(flag == 1) ... bad string


if you're limiting strings to 20 chars, the 420 max length seems like overkill. :P



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.