hi ,i want to know that if i m taking the input from user in C through scanf what should be the code if the requirments are following.
1.should accept string minimum of 3 and maximum of 15 characters.
2.accept alphabets ,blank space hyphen(-)and &
3. first latter should be uppercase.
and another string should
1. accept blank spaces with dot(.)and apostrophe .

Recommended Answers

All 7 Replies

I don't think you can use scanf and meet those criteria, scanf will not accept spaces.

I suggest using fgets and parsing the line you get back to validate.

Even if you could use scanf for your purposes, it is better to use fgets anyway.

All these cant be done in scanf itself so you can make some changes in ur prog to implement them n then move forward which would give the image as if the input is so....

#include<stdio.h>
#include<ctype.h>
int main(void)
{
char str[15];
scanf("%[^\n]",str);
str[0]=toupper(str[0]);
str[15]='\0';
printf("%s",str);
return 0;
}

scanf("%[^\n]",str); Do you want to bet I can overflow the 15 characters that str[] can hold?

I would accept ( if I don't care about any extra left in the buffer ) that if ( scanf("%14[^\n]", str) == 1 ) can fulfill the upper limit, but that's about it.

Posting code requires the use of tags to preserver the format. And it is not optional. Click here to learn how. str[15]='\0'; There's no index 15 in str[], remember that indexing starts at 0, therefore it ends at 14.

scanf("%[^\n]",str); Do you want to bet I can overflow the 15 characters that str[] can hold?

Wat u said regarding the overflow of string array is perfectly right...but my question is can such overflow of array's at such subtle level cause major damages.? if yes how.? and what to refer to avoid such minute but important details...? I mean to brush up the coding skills...

scanf("%[^\n]",str); Do you want to bet I can overflow the 15 characters that str[] can hold?

Wat u said regarding the overflow of string array is perfectly right...but my question is can such overflow of array's at such subtle level cause major damages.? if yes how.? and what to refer to avoid such minute but important details...? I mean to brush up the coding skills...

#1: wat u is not English. Brush up on the forum rules you read when you registered. We speak English on this board, not web/leet-speak

#2: Yes overflow can cause damage, and it's not subtle at all. If your buffer can hold 10 characters and you type in 25, there are 15 bytes that just overwrote data -- ints, floats, and other chars now have data in them they shouldn't have. Use fgets() as suggested.

>but my question is can such overflow of array's at such subtle level cause major damages.?
There's no excuse for mediocrity. Nonetheless, that doesn't prevent our ego from trying to find one.

>and what to refer to avoid such minute but important details...?
To avoid pitfalls: convince yourself there's not such thing as minute details when it comes to programming. Do the thinking that the computer will never do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.