Quick check on reading in + using strings

This is a discussion on Quick check on reading in + using strings within the C Programming forums, part of the General Programming Boards category; Hi I've been writing some C programs with the scanf funciton and wondered if there was an easier way to ...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Posts
    15

    Quick check on reading in + using strings

    Hi

    I've been writing some C programs with the scanf funciton and wondered if there was an easier way to input data. I need functions which can:

    1. Identify set areas in strings (e.g. is the [x]th letter in a 1?)
    2. Read in strings (e.g. if I want to enter 1234 into an array num[4] it can place each number into each array space, so num[0]=1..... num[3]=4)
    3. Count the length of a string (e.g. the string "cisfun" is 6 characters long)

    4. I need to know how spaces and new-line characters (presing the enter key) are handled

    I'm only using the command line.

    Thanks!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    1) not exactly, but look at strtok and strchr. Also string[x] will be xth letter, of course. I don't know what's in it.
    2)Wow, there's a good project for you to work on. 6 lines or less. Simple, honest.
    3)strlen
    4)the ascii value of a space is 32, and a newline is 10.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    993
    Just a note: Except in special cases, 32 and 10 should not be used to represent space and newline in C. Instead, ' ' and '\n' are preferred, for a couple of reasons: No magic numbers, so it's obvious what they stand for; portable to non-ASCII systems.

    If you're writing, say, a charset converter, then it's absolutely necessary to know the values of various characters in various charsets; but for a simple C program, portable constructs should be used.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-24-2009, 05:29 PM
  2. Quick input check question
    By Jozrael in forum C++ Programming
    Replies: 3
    Last Post: 01-20-2009, 07:23 AM
  3. Not working in linux...
    By mramazing in forum C++ Programming
    Replies: 6
    Last Post: 01-08-2009, 02:18 AM
  4. allegro issues
    By mramazing in forum C++ Programming
    Replies: 1
    Last Post: 01-07-2009, 11:56 PM
  5. Help with reading strings and opening files PLEASE
    By green2black in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 05:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21