Finding a string of chars that contain 'x'

This is a discussion on Finding a string of chars that contain 'x' within the C Programming forums, part of the General Programming Boards category; Hi, how do I found out if a name ends with '&' or at worse contains '&'. Below is a ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    31

    Finding a string of chars that contain 'x'

    Hi, how do I found out if a name ends with '&' or at worse contains '&'. Below is a function that gets an inputted line (or name). Is it possible to add functionality to this function that does what I want?

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by metros View Post
    Hi, how do I found out if a name ends with '&' or at worse contains '&'. Below is a function that gets an inputted line (or name). Is it possible to add functionality to this function that does what I want?
    Sorry! Here's the funciton........

    Code:
    int getline(char s[], int lim)
    {
            int c, i;
    
            for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
                    s[i] = c;
            if (c == '\n') {
                    s[i] = c;
                    ++i;
            }
            s[i] = '\0';
            return i;
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Can anyone shed any light on this please?

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    The simplest should be to add an if statement in your for loop that is similar to the one where you check for a newline. In this case you just substitute it for '&', then proceed to do whatever it is you want to do incase of an ampersand, which you did not mention.
    Last edited by Subsonics; 02-16-2010 at 04:33 PM.

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    Quote Originally Posted by Subsonics View Post
    The simplest should be to add an if statement in your for loop that is similar to the one where you check for a newline. In this case you just substitute it for '&', then proceed to do whatever it is you want to do incase of an ampersand, which you did not mention.

    Hi I tested what you said on the following code, however nothing has changed....

    Code:
    int getline(char s[], int lim)
    {
            int c, i;
    
            for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i)
                    s[i] = c;
            if (c == '&') {
                    s[i] = c;
                    ++i;
            }
            s[i] = '\0';
            return i;
    }

  6. #6
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    You did not add an if statement to your for loop, you changed the one you already had outside of the loop.

    You need to do something like this, depending on what you want to do:

    Code:
    for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i){
            if(c == '&'){
                    //do something
            } else 
                   s[i] = c;
    }
    
            if (c == '\n') {
                    s[i] = c;
                    ++i;
            }

  7. #7
    Registered User
    Join Date
    Feb 2010
    Posts
    31
    If you imagine a list



    textfile1
    textfile123
    textfile&
    file&
    file2&


    I then want to run a program that chooses the longest from the list that contains a '&'

    I think I'm not too far away from it, but my head is melted at this stage.

    I'm able to do a computation that selects the longest from the list, but I want to narrow it down to selecting the longest one with '&'. I'm able to discard strings that are less than the longest string but fitting in the & is where I'm struggling. Sorry I'm going around in circles here

  8. #8
    eh ya hoser, got a beer? stumon's Avatar
    Join Date
    Feb 2003
    Posts
    323
    You could look into implementing strchr(const char *s, int c) into your program. That will return a NULL pointer if it doesn't find an instance of c in your string. If it is not NULL, then it found at least 1 and you know there is an ampersand.
    The keyboard is the standard device used to cause computer errors!

  9. #9
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by metros View Post
    I then want to run a program that chooses the longest from the list that contains a '&'
    You asked if your function could be modified to detect an ampersand. The function returns the length of the string. You could choose to return ONLY if an ampersand is found by setting a flag to true in the if statement, else return 0 like this:

    Code:
    int getline(char s[], int lim)
    {
            int c, i;
            bool ampersandFound = false;
    
            for (i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i){
                    if(c == '&')
                            ampersandFound = true;
                    s[i] = c;
            }
    
            if (c == '\n') {
                    s[i] = c;
                    ++i;
            }
            s[i] = '\0';
    
            if(ampersandFound == true)
                    return i;
            else
                    return 0;
    }
    Or you could use strchr() like stumon suggested outside your function when you compare, or inside, there's lots of different ways that this can be achieved.
    Last edited by Subsonics; 02-16-2010 at 07:57 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 03-03-2006, 01:11 AM
  2. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  3. Counting how many chars in a string
    By tigs in forum C Programming
    Replies: 4
    Last Post: 08-05-2002, 12:25 AM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM

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