Thread: How to read character by character without string.h?

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    5

    How to read character by character without string.h?

    Ok,

    PLEASE NO STRING.H

    basically I want to ask the user to input any line of text such as
    Hello World!

    and I want to some how read each character in "Hello World!" one by one.

    I'm not sure if fgets can do the job since it reads the entire line but doesn't exactly read each individual character.

    I'm not sure if I can just do-loop and scanf("%c",&somechar); until it reaches a new line.

    Basically I am attempting to read each character and verify it is in the alphabet using isalpha.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    If you want to read character by character then fgetc would be appropriate. However, you could also stick to fgets and then process the line read character by character with isalpha.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    1,393
    To process input character by character I would use the standard getchar (or fgetc) loop:

    Code:
    int c;
    while ((c = getchar()) != EOF) {
        putchar(c);
    }
    The example reads and echoes a sequence of input. Place any logic or checks you want in the loop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-07-2013, 10:53 AM
  2. character read
    By eajasmin in forum C Programming
    Replies: 1
    Last Post: 03-19-2010, 05:48 AM
  3. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM
  4. need to read a string in one character at a time
    By mclain in forum C++ Programming
    Replies: 6
    Last Post: 12-04-2002, 12:20 AM
  5. Replies: 3
    Last Post: 11-03-2002, 02:14 AM