Using strcmp to find if a string ends in...

This is a discussion on Using strcmp to find if a string ends in... within the C Programming forums, part of the General Programming Boards category; I want to read a string and check if it ends in a .txt I thought about using strcmp to ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    Using strcmp to find if a string ends in...

    I want to read a string and check if it ends in a .txt

    I thought about using strcmp to compare it against a correct string including the .txt but not sure if this would work or is the way it is generally done.

    Essentially I want to use a loop:

    if (string does not end in .txt)
    use strcat to add .txt to end of string
    else
    nothing

    Could anyone explain how to do this?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Just compare the last 4 characters of the string:

    if((len = strlen(str)) > 3 && !strcmp(str + len - 4, ".txt"))
    If you understand what you're doing, you're not learning anything.

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by frankchester View Post
    I want to read a string and check if it ends in a .txt

    I thought about using strcmp to compare it against a correct string including the .txt but not sure if this would work or is the way it is generally done.

    Essentially I want to use a loop:

    if (string does not end in .txt)
    use strcat to add .txt to end of string
    else
    nothing

    Could anyone explain how to do this?
    Subtract the length of the "pattern" from the length of the "target" to obtain an index. Then simply use strcmp to compare "pattern" with the address of that index of "target". Make sense? Just be sure that the index isn't negative, obviously...



    ITSA
    Socket Library!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by itsme86 View Post
    Just compare the last 4 characters of the string:

    if((len = strlen(str)) > 3 && !strcmp(str + len - 4, ".txt"))
    Would you be able to help me out with some psuedocode? I'm new to programming so I find it really hard to understand what code is actually doing if its complex :/

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ummm... why wouldn't you just use strstr() ?

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by CommonTater View Post
    Ummm... why wouldn't you just use strstr() ?
    What does that do? I hadn't come across it in the string tutorial.

    As I said, I don't know what the solution is, thats why I'm here asking.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by CommonTater View Post
    Ummm... why wouldn't you just use strstr() ?
    Probably because some annoying person would then see what would happen with "string.txt.c". (Of course, if you put the index check back, then you're okay again.)

    And at OP: there's no substitute for being able to read the code. When you see something with a lot of punctuation in it, which that line has, it probably helps to use that to break the code up into groups (things inside the parentheses, things on one side/the other side of the &&, etc.) And then look at each group and see what it does, then put it all together.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by tabstop View Post
    Probably because some annoying person would then see what would happen with "string.txt.c". (Of course, if you put the index check back, then you're okay again.)

    And at OP: there's no substitute for being able to read the code. When you see something with a lot of punctuation in it, which that line has, it probably helps to use that to break the code up into groups (things inside the parentheses, things on one side/the other side of the &&, etc.) And then look at each group and see what it does, then put it all together.
    Hi,

    I'm trying to, the problem is I find it difficult to work out what are variables that have been used created and what are actual programming terms and functions, as I obviously don't know much about programming or code at all (I've only been doing this for a month or so with no programming knowledge before)

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    You can use strstr, but you'd need to call it in a loop, incrementing the returned pointer each iteration until you've reached the last occurrence. The logic is a little messy, too, IMO (well, compared to the methods already described, anyway), in that it requires a save/swap approach with the pointers involved.



    ITSA
    Socket Library!

  10. #10
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Quote Originally Posted by tabstop View Post
    And at OP: there's no substitute for being able to read the code. When you see something with a lot of punctuation in it, which that line has, it probably helps to use that to break the code up into groups (things inside the parentheses, things on one side/the other side of the &&, etc.) And then look at each group and see what it does, then put it all together.
    For the record, I wrote it that way in case it was an assignment. I wanted the OP to have to think about it and rewrite it in a way that wasn't suspicious to the instructor.
    If you understand what you're doing, you're not learning anything.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Quote Originally Posted by itsme86 View Post
    For the record, I wrote it that way in case it was an assignment. I wanted the OP to have to think about it and rewrite it in a way that wasn't suspicious to the instructor.
    Yeah, I thought it struck a good balance, too (and the best kind of demonstration, IMO).



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to compare string with 'getter' returned string.
    By Swerve in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2009, 05:56 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM

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