Like Tree2Likes
  • 1 Post By oogabooga
  • 1 Post By slingerland3g

string suffix

This is a discussion on string suffix within the C Programming forums, part of the General Programming Boards category; Please have a glance. It has to find if the first string suffix of second one. e.g. 1st world 2nd ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    22

    string suffix

    Please have a glance. It has to find if the first string suffix of second one.
    e.g. 1st world
    2nd hello word
    hey-you done it! it is a suffix and so on

    Code:
    #include <stdio.h>
    
    int  strlength(char *string)
    {
        int i=0;
        while (string[i]) i++;
        return i;
    }
    
    int issuffix(char * string1, char * string2)
    {
    
        int i,e=0, rest;
        rest= strlength(string1)-strlength(string2);
        if (strlength(string1) > strlength(string2))
            return 0;
        for (i=rest; string1[i]; i++)
            for (e=0; string2[i]; e++)
            {
                if (string1[i] != string2[e])
                    return 0;
            }
    
    
        return 1;
    
    }
    
    main()
    
    {
        char stringA[1024], stringB[1024];
        int n;
    
        printf("string1: ");
        scanf("%c[^\n]", stringA);
    
        printf("string2: ");
        scanf("%c[^\n]", stringB);
    
        n=issuffix(stringA,stringB);
        if (n)
            printf(" \"%s\" is a suffix of %s\n",stringA, stringB);
        else
            printf(" \"%s\" is NOT a suffix of %s\n",stringA, stringB);
    
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    string1 and string2 are bad names. Which is the potential suffix? Why not call that one ... suffix?

    I'm assuming you're "not allowed" to use strlen; otherwise, use it.

    If the first string to issuffix is supposed to be the potential suffix (as implied by main), then you seem to be mixing them up in calculating rest. It should be length of string2 minus length of string1.

    And there's no reason to computer their lengths again after you've calculated rest. Just check if rest is negative.

    And you only need a single loop in issuffix, not a double loop.

    EDIT: I forgot to mention that your scanf's should be like this:
    Code:
        scanf(" %[^\n]", stringA);
    Note the space before the % (and the lack of the c).
    Last edited by oogabooga; 03-14-2012 at 03:28 PM.
    ph3s likes this.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    22
    Thanks for valuable coments. This was a test q. today. failed successfully. Can you or anyone else tell me please about the space before %. What does it do?
    Last edited by ph3s; 03-14-2012 at 04:30 PM.

  4. #4
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Please note that

    Code:
    while (string[i]) i++;
    can be written as

    Code:
    while (string[i++]);
    I believe the strlen() is done this way as well. As far as the space before the expression " %[^\n]", does your string you are passing in contain a space at the beginning followed by the carriage return? When using scanf() , you really must know how your data is presented to that function for proper parsing.
    ph3s likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-12-2006, 05:50 AM
  2. What does the suffix * mean?
    By flaran in forum C++ Programming
    Replies: 14
    Last Post: 10-18-2005, 10:09 PM
  3. Suffix Trees
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2005, 01:45 PM
  4. suffix help.
    By milkyway_sushi in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2004, 05:41 PM
  5. Suffix Array
    By bondinc in forum C Programming
    Replies: 1
    Last Post: 10-02-2002, 06:43 PM

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