Thread: string question

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    90

    string question

    Hi all programmers , i was wondering if i read a string and then i was trying to print the first letter of that string what am i supposed to do ?

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    puts(string[0]); should work.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    well i tried that lol , it doesnt

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    main()
    {
    char string[20];
    gets(string);
    puts(string[0]);
    getch();
    }

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Apart from using gets, what "doesn't work" about that?

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    try it on a compiler, it gives me an error at the line puts(string[0]);
    invalid conversion from a char to const char !!! i dont have a clue about the problem.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by everyone0 View Post
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    main()
    {
    char string[20];
    gets(string);
    puts(string[0]);
    getch();
    }
    Well, you could try putc(string[0]); printf(" First letter is... %c",string[0]); or others...

    The point is that strings in C are arrays of characters. The first character is array[0] the last one is always sizeof(array)-1. The only thing that separates them from arrays of bytes is the trialing 0 at the end of each. There are a number of library functions that treat arrays of characters as strings of text so you use them...

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Use the normal print routines:
    Code:
    printf("%c\n", string[0]);
    and for that matter use the normal input routines:
    Code:
    fgets(string, 20, stdin);
    EDIT: Also, reading the error message as it is actually written will help: conversion to const char * is a lot different than conversion to const char.

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    90
    mm sorry about that

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by everyone0 View Post
    mm sorry about that
    No need to apologize... the very best learning experience you can have is learning how to fix your mistakes...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Reusing a string pointer question
    By chiefmonkey in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2009, 04:53 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 06:44 PM