Thread: Determining if a string has an "s" at the end

  1. #1
    Registered User
    Join Date
    Apr 2011
    Location
    USA
    Posts
    2

    Determining if a string has an "s" at the end

    Hi, I'm pretty new to programming. I'm trying to make a program to determine if input from the user is plural or not, and based on that, print a response with the correct grammar, but it isn't working.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <strings.h>
    #include <windows.h>
    
    int main()
    {
        char name[10];
        char food[10];
        char *p;
        p = (strlen(food)-1);
        /*int plural = strlen(food); */
    
        printf("What is your name?\n");
        scanf("%s", &name);
        printf("Hello %s.\n");
        Sleep(1000);
        printf("What is your favorite food?\n");
        scanf("%s", food);
        /* if (food[plural]="s") {
        printf("Gross! %s are disgusting!\n");
        }
        else if (food[plural-1]!="s") {
        printf("Gross! %s is disgusting!\n");     I tried this part first and it didnt work so I  
                                                            .tried the pointers which I dont really  
                                                             .understand */
        }
        return 0;
    }
    Whenever I run the program, no matter how many "s"'s I put in or dont put in, the always returns, "Gross! ___ are disgusting!" What am I doing wrong?

    I tried with pointers and with a string length variable (the one that's hidden), but there's probably several ways to do it.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Put 's' rather than "s"
    The former is a single character, whereas the second is a string (with a single character).
    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.

  3. #3
    Registered User Raj_55555's Avatar
    Join Date
    Apr 2011
    Location
    in her heart
    Posts
    1
    To add to that it's not strings.h, its string.h. I don't think you need to use windows.h here.
    And what on earth are you trying to achieve by storing the length of a string into a character pointer?
    Code:
    p = (strlen(food)-1);//ERROR
    There are numerous other errors. In short you are being too lazy to post a code that won't compile (and didn't even mention it).
    At least you used the code tags.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    Don't make this more difficult than necessary. You were on the right track when you were using the strlen() function. The problem with your approach is that strlen() returns an unsigned integer value, and you are attempting to assign this to a pointer variable. That's a no-no.

    As you surmised, we can use strlen() to return the length of the string. The character before the null character is one less than the length of the string. So,

    Code:
    char c = food[strlen(food)-1];
    will return the last character of the string and store it in the variable c.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    if (toupper(food[strlen(food) - 1])  ==  'S')
      { puts("Please don't pluralize food names!");
        food[strlen(food) - 1] = 0; }
    puts (food);
    Last edited by CommonTater; 04-18-2011 at 01:40 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by kmess View Post
    As you surmised, we can use strlen() to return the length of the string. The character before the null character is one less than the length of the string. So,

    Code:
    char c = food[strlen(food)-1];
    will return the last character of the string and store it in the variable c.
    What happens when strlen returns 0?


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

  7. #7
    Registered User
    Join Date
    Apr 2011
    Location
    USA
    Posts
    2
    CommonTater, what does toupper and puts do? kmess, what can i do with that variable c? maybe:
    Code:
    if ( c == 's' ) {
       printf( "Gross! %s are disgusting!" )
    would that work?

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Did you know that the internet has places you can search for things? If I were a betting man, I would bet that if you were to go to one of those "search places" and you typed in "toupper", you would have had your answer faster than you could have typed up that reply. I will leave that as an exercise to the reader.

    But no, that won't work, because you have a format specifier, with no provided argument, and you don't have a semicolon at the end of your printf line. Also, you would be murdering the English language with your abuse of plurals and lack of appropriately placed apostrophe.


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

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Seany242 View Post
    CommonTater, what does toupper and puts do? kmess, what can i do with that variable c? maybe:
    Code:
    if ( c == 's' ) {
       printf( "Gross! %s are disgusting!" )
    would that work?
    Look them up in your C library documentation... they're standard functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  2. "itoa"-"_itoa" , "inp"-"_inp", Why some functions have "
    By L.O.K. in forum Windows Programming
    Replies: 5
    Last Post: 12-08-2002, 08:25 AM
  3. "CWnd"-"HWnd","CBitmap"-"HBitmap"...., What is mean by "
    By L.O.K. in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 07:59 AM

Tags for this Thread