Thread: string problem!

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    14

    string problem!

    nid help in...
    char str[6]
    input is hello...
    so it will be like this
    [h][e][l][l][o] ryt?

    how can i print each element seperately?
    print h...
    then e. then l. then l. then o...
    individually...

    tnx....

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Use a for loop:

    Code:
    char string [20];
    int i;
    
    scanf ("%s", string);
    
    for (i=0; i < strlen(string); i++)
    {
          printf ("%c", string [i]);
    }
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Things to avoid:
    Code:
    scanf ("%s", string);
    Without specifying the maximum size, this has the same problem as gets.

    Learn to avoid this construct lest some day you use this idiom with a very long string.
    Code:
    for (i=0; i < strlen(string); i++)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    14
    what is the function of strlen?!
    and before using it do i need to do?
    #include<string.h>????

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by rothj0hn
    what is the function of strlen?!
    Find the string length.

    [edit]strlen
    The function returns the number of characters in the string s, not including its terminating null character.
    [/edit]

    You should try to discover your compiler's Help/man pages and/or find a decent online reference for standard functions (happy googling).
    Last edited by Dave_Sinkula; 02-06-2006 at 10:44 AM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM