Thread: Very very basic: How to display one character in a string?

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    20

    Very very basic: How to display one character in a string?

    It's been about two years since I last program c, now I need to do it for a basic project. How would I print out one letter in a string?

    For example lets say I have a string called str=[Hello]. I want to display the third letter so just the "l".

    Here's what I have so far

    Code:
    #include<stdio.h>
    int main()
    {
        char str[50] = "Hello";
        printf("The third letter is : %s \n",str[3]);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Posts
    20
    I solved it by using "%c" instead of "%s" stupid me.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Code:
    printf("The third letter is : %s \n",str[3]);
    This will print the fourth letter (after changing %s to %c of course). Remember array indexing starts at 0.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-17-2013, 01:24 PM
  2. How can I display the space character?
    By cyclon in forum C Programming
    Replies: 4
    Last Post: 05-20-2011, 06:17 PM
  3. Basic question on character array
    By vandrea in forum C Programming
    Replies: 6
    Last Post: 09-20-2009, 12:05 AM
  4. display character size...(quite urgent..)
    By karthi in forum C Programming
    Replies: 10
    Last Post: 07-11-2007, 09:42 PM
  5. comparing character in a string to anothr character
    By merike in forum C Programming
    Replies: 5
    Last Post: 05-11-2007, 12:16 AM