Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ C
#include "stdio.h"
void main()
{
	char a[20];
	scanf("%s",a);
	printf("%s",a);
}
 
I want to input less than 20 characters, how to control the number I input?
if the number is more than 20, the program should give me a warn, I want to know how to do this,Thank you.
Posted 14-Dec-12 0:29am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

With scanf() and strings, you should always pass the width to specify the max. number of chars to be stored (pass one less than the size of the char array to store the terminating NULL char). So with max. 20 chars use:
char a[21];
scanf("%20s",a);
To get the number of chars entered, use the strlen() function afterwards. To show a warning when too much chars has been entered, allow one more character to be scanned and compare the final string length:
char a[22];
scanf("%21s",a);
if (strlen(a) > 20)
{
    // show warning here
}
  Permalink  
Comments
CPallini at 14-Dec-12 7:28am
   
5 from 'fast' Carlo :-)
Espen Harlinn at 17-Dec-12 12:36pm
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You may use the width specifier (see the security note in the MSDN documentation[^]), e.g.
scanf("%19s",a);
 
(and always check scanf result)
  Permalink  
v2
Comments
Jochen Arndt at 14-Dec-12 6:52am
   
+5. Beat me by a minute.
Angela2012 at 14-Dec-12 20:03pm
   
Thank you ,but I want to know if it is possible to check the number of characters automatically using scanf,when the number is larger than 20,it will give me a warn immediately rather than I finish inputting a very very long string.
CPallini at 15-Dec-12 4:58am
   
No, it is not possible.
Mohibur Rashid at 15-Dec-12 5:16am
   
it is not possible with scanf is true
Espen Harlinn at 17-Dec-12 12:36pm
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Comments
CPallini at 14-Dec-12 7:29am
   
5, even if you're advertising 'The Competitors' :-)
Tadit Dash at 14-Dec-12 7:51am
   
Thanks a lot @CPallini.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

#include <stdio.h>
#include <string.h>

void main()
{
	char a[20];
        int i, c=1;
        while(c == 1)
        {
              if(strlen(a) > 19)
              {
                   printf("WARNING\n");
                   printf("DANGER FOR BUFFER OVERFLOW");
              }
              printf("Give the next char: ");
	      scanf("%s", &a[i]);
	      printf("Scanned char: %c\n",a[i]);
              printf("If you want to input another char type 1 or type another number to exit");
              scanf("%d", &c);
              i++;
        }
}
  Permalink  
v2
Comments
Angela2012 at 14-Dec-12 19:53pm
   
Thank you very much,in your solution "strlen(a)" is always larger than 19
Snk Tay at 15-Dec-12 5:11am
   
strlen(a) is not always larger than 19. Because the array a[20] is empty at the begining and the lefth of array getting bigger when you add a char.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 6,725
1 OriginalGriff 6,696
2 CPallini 5,315
3 George Jonsson 3,589
4 Gihan Liyanage 2,650


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 15 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100