Thread: matching strings at end

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    115

    matching strings at end

    Hi guys,
    Anyone see whats wrong here?
    What im trying to do is input two strings say s and t.
    Return true if the string s is at the end of string t.
    My function just keeps return true.

    cheers!

    Code:
    #include <stdio.h>
    #include <string.h>
    #define SIZE 1000
    
    int strend( char [], char [] );
    int main()
    {
    	char s[SIZE];
    	char t[SIZE];
    	printf( "Enter s: " );
    	fgets( s, SIZE, stdin );
    	printf( "Enter t: " );
    	fgets( t, SIZE, stdin );
    
    	if ( strend( s, t ) )
    		printf( "string s occurs at end of string t\n" );
    	else
    		printf( "string s does not occur at the end of string t\n" );
    	return 0;
    }
    int strend( char s[], char t[] )
    {
    	int i;
    	int j;
    	
    	for ( i=strlen(t)-strlen(s); i<strlen(t)-2; ) {
    		printf( "%d\n", i );
    		for( j=0; j<strlen(s)-2; ) {
    			if ( t[i] == s[j] )
    			{
    				i++;
    				j++;
    				break;
    			}
    			else {
    			return 0;
    			}
    		}
    	}
    	return 1;
    }
    there are only 10 people in the world, those who know binary and those who dont

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Anyone see whats wrong here?
    Your doing it the hard way?
    Code:
    #include <string.h>
    
    enum { NO, YES };
    
    int strend(const char *s, const char *t)
    {
        size_t slen = strlen(s);
        size_t tlen = strlen(t);
        int    rc;
    
        if (slen < tlen)
            rc = NO;
        else if (strcmp(s + (slen - tlen), t) == 0)
            rc = YES;
        else
            rc = NO;
    
        return rc;
    }
    Just check if the length of s is less than the length of t, if so s cannot contain t. Otherwise, subtract the length of t from the length of s and use that location as the first argument to strcmp. Boom! Instant strend logic with minimal effort on your part. The hardest part is to remember to check the lengths first.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    115
    =(

    forgot about strcmp

    damnit

    thanks guys!
    there are only 10 people in the world, those who know binary and those who dont

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. Adventures in labyrinth generation.
    By guesst in forum Game Programming
    Replies: 8
    Last Post: 10-12-2008, 01:30 PM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  4. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM
  5. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM