Very simple question...annalyze strings

This is a discussion on Very simple question...annalyze strings within the C Programming forums, part of the General Programming Boards category; Hey, I know this is basic but its the only thing I need to have my program working for school! ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    16

    Very simple question...annalyze strings

    Hey,

    I know this is basic but its the only thing I need to have my program working for school!

    I have a char buff[512].

    Theres a possibility the first characters might be "exit" ... with a possibility of other stuff behind it.

    I need to know IF the first four characters are "exit".

    How do I do that?

    Simple question, sorry!

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You can compare each of the first four characters, or you can use strncmp.

  3. #3
    MTK
    MTK is offline
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    A simple way to do it:

    Code:
    if(buff[0]=='e' && buff[1]=='x' && buff[2]=='i' && buff[3]=='t') {
            //Do something
    }

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    26
    As tabstop said probably u could use strncmp. Follg is the code thou a littl longer I guess

    Code:
    char exit_ca[5];
    memset(exit_ca, 0x00, 5);
    strncpy(exit_ca, "exit", 5);
    
    if(ZERO == strncmp(buff, exit_ca, strlen(exit_ca)))
    {
     printf("first four characters are exit");
    }
    Last edited by vandrea; 09-14-2009 at 01:18 PM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by vandrea
    Code:
    char exit_ca[5];
    memset(exit_ca, 0x00, 5);
    strncpy(exit_ca, "exit", 5);
    Your array is too small.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    26

    Your array is too small.
    quzah i did not get your point. isnt 5 sufficient since we would store only exit and a null character at the end , i thought it would be enough?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, I was too tired. I thought the word exit had five letters for some reason. (I'd been up for like 30 hours so, that's my excuse!)


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Sep 2009
    Posts
    6
    Code:
    Hi,
    You can use the c++ method  strncmp()!
    
    char exit[5] = "exit";
    
    if(!strncmp(buff, exit, 4))
    {
           cout << "the first four characters are "exit"!";
    }
    
    Carle
    _____________________
    Free chat software for you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple question regarding variables
    By Flakster in forum C++ Programming
    Replies: 10
    Last Post: 05-18-2005, 09:10 PM
  2. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  3. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  4. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

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