Printing only certain characters

This is a discussion on Printing only certain characters within the C Programming forums, part of the General Programming Boards category; Hello Everyone, Is there a command that will print only certain characters. I am looking at the strstr command but ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    42

    Printing only certain characters

    Hello Everyone,

    Is there a command that will print only certain characters. I am looking at the strstr command but not sure if this is right.

    What I am trying to do is have the user enter a string of characters and then print only ones that end in 'ed'.

    I also looked at tokenized and then use strstr to parse out only strings that end in 'ed'. Hopefully I am on the right track.

    An advice would be appreciated

    Thanks

    DMKanz07

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    Sounds like a good plan to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Okay I am stuck on how i can read a series of strings and print only those strings that ended with the letters "ed"

    Here is what I thought I could do is find the length of the string and the validate the last two characters. But I am not sure how to do that.

    Oh C gods show me the way

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* Function main begins program execution */
    int main()
    {
    
        char s [ 100 ];
        int i;
        printf( "Enter a line of text: " );
    
        /* Use gets to read line of text */
        gets( s );
        printf( "\n" );
        
        for( i = 0; i < strlen ( s ); ++i ) {
            printf( "%c\n", s [ i ] );
        }
        
        printf( "%lu\n", strlen( s ) );
          
        /* Indicates successful termination */
        return 0;
    
    } /* End Main */
    Thanks

    DMKanz07

  4. #4
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Ok. Again, don't use gets(). It's bad news.

    But for what you want to do, use strlen to find the length of the string and suppose you store that in len.

    After that, you know that s[len - 1] will be the place where there is a null character.

    Therefore, s[len - 2] is the last character and s[len-3] is the second to last. Done.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    heres an example
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
        char s[10];
        char last, secondLast;
        int len;
        
        fgets(s,sizeof(s),stdin);    
        
        len = strlen(s);
    
        if (len < 3)
        {
               printf("enter word with at least 2 characters\n");
               return 1;
        }
        
        last = s[len - 2];
        secondLast = s[len - 3];
     
        printf("last char is: &#37;c\n", last);
        printf("secondLast char is: %c\n", secondLast);
       
        // maybe use some if statements to compare these last two characters?
    }

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Okay I have been trying to figure out how you test to see if the last to character are equal to 'ed' and I am stumped

    I tired

    Code:
    if ( secondLast = "e" && last = "d" )
        printf( "%c\n", s [ i ] );
    but it does not compile. I am not understanding this very well. Sorry guys.

    DMKanz07

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    when you say 'it does not compile' always remember to post why it doesnt compile! ie, what does the compiler complain about?

    in this case, secondLast and last are of type 'char'. when you assign values to chars or check their values, use single quotes.
    Code:
    char myChar = 'a';
    hope it helps

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    > if ( secondLast = "e" && last = "d" )
    1. Use == for comparing, not =
    2. A single character uses single quotes ('d'), not double quotes ("d")

    Also, I thought you were going to work in 2 stages
    - get a list of all the words
    - print those which end in "ed"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Fear the Reaper...
    Join Date
    Aug 2005
    Location
    Toronto, Ontario, Canada
    Posts
    625
    Code:
    if ( secondLast = "e" && last = "d" )
        printf( "%c\n", s [ i ] );
    I don't know why there's an i in there. You don't have to loop through all the characters in the string. The last and second last are always given by what I've told you beforehand. Always.
    Teacher: "You connect with Internet Explorer, but what is your browser? You know, Yahoo, Webcrawler...?" It's great to see the educational system moving in the right direction

  10. #10
    Registered User
    Join Date
    Mar 2007
    Posts
    42
    Nadroj,

    I am but I just trying to get one word to work first. Dang I know == compares dont know why I used =.

    C is still very new to me, i should say programming is new to me

    Thanks for your patience

    DMKanz07

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 02-21-2009, 01:55 AM
  2. Replies: 8
    Last Post: 12-06-2008, 01:43 PM
  3. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  4. printing non-ASCII characters (in unicode)
    By dbaryl in forum C Programming
    Replies: 1
    Last Post: 10-25-2002, 01:00 PM
  5. Printing extended ASCII characters
    By Jonny M in forum C Programming
    Replies: 2
    Last Post: 03-01-2002, 09:12 AM

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