Thread: char question

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    char question

    Hello, have been trying to create a program where someone introduces a letter and the program told you if the letter is the same as one or more letters from a word which is in the program, for example the word is "Going", what I want is that someone writes a character and the program compares with each one of the word, and if it is the same of any in the original string, i wikk print it, however i want it to do it without caring if they write a capital letter or not, for example if they write "I", the program prints --i-- (from going) and if they then press "g", to appear G-i--
    is there a way to do this?, please if anybody knows a way to do so i will be really thankfull

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    You'll want to have a look at the functions in ctype.h. One idea is to check each character with islower(), and if it is, toupper() it.

    http://www.infosys.utas.edu.au/info/...b.html#ctype.h

    edit: For an extra challenge, you could also do it manually. At the binary level, every char is really just an integer representing thr ASCII number of the character.

    hint: (int)'A' - (int)'a' == (int)'Z' - (int)'z'
    Last edited by sean; 11-13-2004 at 01:08 PM.

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    so you want hangman without the hanging man? Look at this to get some ideas on how to do it: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Bah! What fun is hangman is there's no fear of death in the process?

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Programming fun perhaps??

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, but wouldn't it be funner if you had the fear of death if you programmed incorrectly? See...

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by sean_mackrory
    You'll want to have a look at the functions in ctype.h. One idea is to check each character with islower(), and if it is, toupper() it.

    http://www.infosys.utas.edu.au/info/...b.html#ctype.h

    edit: For an extra challenge, you could also do it manually. At the binary level, every char is really just an integer representing thr ASCII number of the character.

    hint: (int)'A' - (int)'a' == (int)'Z' - (int)'z'
    Hey Sean not every computer uses ASCII and the values of the letters are not guaranteed to be contiguous. Also there is no need to cast character literals to int.

  8. #8
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    not every computer uses ASCII
    Good point - it will be OS specific - but if you're just using a Windows machine and the only purpose of this program is for fun or learning, it should work. THough it is a good idea to start thinking about portability even as you're learning C.

    there is no need to cast character literals to int.
    [lie]I just thought it would make the conecept clearer for the OP![/lie]

    [truth]Darn... thanks Thantos![/truth]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char pointer to pointer question
    By Salt Shaker in forum C Programming
    Replies: 3
    Last Post: 01-10-2009, 11:59 AM
  2. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  3. what does this warningmean???
    By kreyes in forum C Programming
    Replies: 5
    Last Post: 03-04-2002, 07:53 AM
  4. errors in class(urgent )
    By ayesha in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2001, 10:14 PM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM