Thread: A question about inputting a string

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    A question about inputting a string

    If I need to write a program that reads a string (a person's name)
    then print out the first five characters of the name, how can i do that?

    for example:
    johnny will output johnn

    this is what I wrote

    Code:
    int main () {
    	
    	char input[5];
    	
    	while (scanf("%s\n",input) != EOF) {
    		
    		printf("%s\n",input);
    		
    	}
    
    	return 0;
    
    }
    however, if the user inputs more than 5 characters, then the array for input will not be large enough to hold...
    but at the same time, i can't restrict the number of characters that a user type in...
    so, any hints?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    If you don't know how many character will be input-ed you need to read character by character and put it into a dynamically allocated string. When the string gets filled up, you reallocate more memory for it.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Quote Originally Posted by claudiu View Post
    If you don't know how many character will be input-ed you need to read character by character and put it into a dynamically allocated string. When the string gets filled up, you reallocate more memory for it.
    So, instead of scanf("%s\n,input), I need to use %c?
    and what size of char input[] should I use inside the bracket..?

    Would you mind if you can give me an example..?

    oh...and I am not allowed to use the heap storeage
    Last edited by winggx; 03-27-2010 at 11:28 PM.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I believe BUFSIZ is the max that one call of scanf will be able to retrieve.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by winggx View Post
    but at the same time, i can't restrict the number of characters that a user type in...
    but you can restrict number of chars you will read at once

    Code:
    char buf[100];
    
    while(scanf("%99s", buf) == 1)
    {
       printf("%s", buf);
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Mar 2010
    Location
    pkr
    Posts
    32
    Can not the problem be solved by using "pointer char String" and "loop+array" to display 5 character

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  2. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM