how do i compare first letters in two strings?

This is a discussion on how do i compare first letters in two strings? within the C Programming forums, part of the General Programming Boards category; I have two pointers to a string Code: char *name1; char *name2; user imputs the names. How do i use ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    18

    how do i compare first letters in two strings?

    I have two pointers to a string

    Code:
    char *name1;
    char *name2;
    user imputs the names. How do i use strcmp function to compare the first letters from two strings, so the function gives me TRUE if name1 and name2 begin with the same letter?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't. strcmp will compare the whole entire strings no matter what.

    Bonus hint: the first letter of name1 is name1[0] and the first letter of name2 is name2[0].

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    18
    it does now compile. It says "passins arg 2 of strcmp makes pointer from integer without a cast"

    Im using pointers to a string (i have to).

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    69
    If you ever need to compare a fixed-length of characters in a string, you can use strncmp(); Its the same as strcmp but it takes a third argument, which is the number of characters to compare.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Nothing wrong with using pointers to a string, but try and pay attention: you cannot solve this problem with strcmp. Strcmp operates on an entire string, not just the first character. To test characters for equality, you use ==.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    Quote Originally Posted by MalickT View Post
    it does now compile. It says "passins arg 2 of strcmp makes pointer from integer without a cast"

    Im using pointers to a string (i have to).
    Sigh. Why is it so difficult to understand these matters?
    Read: http://cpwiki.sourceforge.net/A_pointer_on_pointers
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Here's a hint. This is how you would compare the 3rd letter in two strings:
    Code:
    if ( str1[2] == str2[2] )
    Now if you still can't figure out how to compare the 1st letters, well then I give up.

  8. #8
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    Surely, you can use strncmp to do that.
    eg.
    strncmp (name1, name2,1)

    it returns 0 zero if they are equal to each other, or less than zero if first one less than second, or greater than zero if it is greater.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by velid View Post
    Surely, you can use strncmp to do that.
    eg.
    strncmp (name1, name2,1)

    it returns 0 zero if they are equal to each other, or less than zero if first one less than second, or greater than zero if it is greater.
    Yes, you could. But why would you want to? That would be like buying a top of the line $3000 computer with all the bells & whistles and using it just to check E-mail.
    Simple is better.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compare strings problem
    By chelito19 in forum C Programming
    Replies: 2
    Last Post: 04-16-2009, 09:01 PM
  2. Compare Two Strings?
    By Paul22000 in forum C++ Programming
    Replies: 9
    Last Post: 05-24-2008, 06:09 PM
  3. Need some help with strings
    By aldin176 in forum C++ Programming
    Replies: 2
    Last Post: 11-07-2006, 02:13 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  5. Replies: 3
    Last Post: 04-16-2004, 12:32 PM

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