Scan a string, present character

This is a discussion on Scan a string, present character within the C Programming forums, part of the General Programming Boards category; Hello, This is my first time here, i'm taking a programming class and no one was able to help me ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Location
    Arlington, Tx
    Posts
    3

    Post Scan a string, present character

    Hello,

    This is my first time here, i'm taking a programming class and no one was able to help me with my question.

    I want to scan a string and then display each character of the string on a separate line.
    i can't figure out the command to use, i think we should use a function recall.

    i can't even figure out how to do the flowchart on it. any ideas?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome to the forum, emdleb!

    (From the home of Cowboy stadium)

    Oh! That's tooo easy.

    Do it two ways: iteratively, and recursively.

    include <string.h> for string handling

    if string = "Abbracadabbra", then
    get it's strlen(string), and say it's length
    then

    for(i=0;i<length;i++)
    ....print the string[i] and a \n (newline char).

    using the right %c format for printf(), of course.

    for input: scanf("%s", string) is normal. Scanf() has all kinds of format specifiers you can use to tweak the string for length, stop storing the char's with a certain letter, etc. Watch scanf() though, it will quit on you unexpectedly. Hates spaces! It's return value (an int), can be used to help moderate it's quirkyness.
    fgets() is the better choice for user input, generally.


    I'm leaving you with work to do, quite on purpose.
    Last edited by Adak; 02-11-2011 at 06:38 AM.

  3. #3
    Registered User
    Join Date
    Feb 2011
    Location
    Arlington, Tx
    Posts
    3

    Post

    Adak! thanks for the fast reply! this is really great! i'm glad i signed up!

    okay so i followed your steps, and i got stuck with the correct %c format
    the lesson i'm working on is function, so somewhere i should include a function,
    how is it possible to make a function with string/character?

    here's what i have:


    #include <stdio.h>
    #include <string.h>

    int main ()
    {
    int i;
    int length;
    char string[50];
    printf ("Enter a sentence: ");
    gets (string);
    printf ("The sentence entered is %u characters long.\n",strlen(string));
    strlen(string) == length;

    for(i=0;i<length;i++)
    printf("%c\n", string); /* i'm having a problem here i don't know which %c format to use*/
    getch();
    }

    Thank you

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void)  <---- correct format for main()
    {
    int i;
    int length;
    char string[50];
    printf ("Enter a sentence: ");
    
    gets (string);   <----  scanf("%49s", string); is safer... no buffer overflows
    
    
    printf ("The sentence entered is %u characters long.\n",strlen(string));
    
    
    strlen(string) == length;    <---- you are assigning this incorrectly.
    
    
    for(i=0;i<length;i++)
    printf("%c\n", string[i]);   <--- you need the array index
    
    
    
    getch();
    }

  5. #5
    Registered User
    Join Date
    Feb 2011
    Location
    Arlington, Tx
    Posts
    3
    Okay sweet! i got it!
    Thank you guys!

    does anyone know by any chance what is the correct command to use for Raptor (flowchart) ?

  6. #6
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I want to scan a string and then display each character of the string on a separate line.
    If your 'string' contains space, you want to use fgets().

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by emdleb View Post
    Okay sweet! i got it!
    Thank you guys!

    does anyone know by any chance what is the correct command to use for Raptor (flowchart) ?
    No, not familiar with it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  5. Replies: 3
    Last Post: 11-03-2002, 01:14 AM

Tags for this Thread


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