#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0

    The end of the word - XYZ


    Need to develop program, which can print to screen words, which have ends - XYZ. But it's will be massive of strings. For example, initialization:
    Code:
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    int main (void)
       { int n, i, j, k;
         printf ("Enter n\n");
         scanf ("%d", &n);
         char str[n+1][11];
         printf ("Enter words\n");
         for (i=0; i<n+1; i++)
              gets (str[i]);
         for (i=0; i<n+1; i++)
              puts (str[i]);
    system ("pause");
    return 0;
    }
    I have an idea. We can move from character space. For example, we began move and when we met with character space we deduct one position and compare it with letter "z", then deduct another position and compare it with letter "Y", and then again deduct one position and compare it with letter "X". If it's true, print word into screen. Any ideas how it realize ? Thanks.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    That's one way. The only other ways I could think of would be variations on that same theme, so you're on the right track.

    Think about the format of that input. Are spaces the only characters that will separate words (AKA "delimiters")? What about newlines? What about punctuation marks such as commas and periods? Draw up a list of all possible delimiters so that you will know what to test for.

    Also, are you looking for a final "XYZ" or a final "xyz"? In C, case matters. Does case also matter in your assignment? That will make a difference in your program.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    2
    Rep Power
    0
    Originally Posted by dwise1_aol
    That's one way. The only other ways I could think of would be variations on that same theme, so you're on the right track.

    Think about the format of that input. Are spaces the only characters that will separate words (AKA "delimiters")? What about newlines? What about punctuation marks such as commas and periods? Draw up a list of all possible delimiters so that you will know what to test for.

    Also, are you looking for a final "XYZ" or a final "xyz"? In C, case matters. Does case also matter in your assignment? That will make a difference in your program.
    Oh, yes. There are small letters "xyz". And it will be one word in one line. Without punctuation marks.

    To verify the condition, it is necessary to know the length of the word. For example the word length 5, then we need to check the 4, 3 and 2, the symbol and compare them with the "xyz". But how do you know the length of each word in the array?
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    If it's one word per line, then there might not be a space between the word and the newline character. Also, whether there would even be a newline character depends on how you're reading your input. For example, gets leaves the newline off, whereas fgets keeps it on.

    Also, you shouldn't use gets, but rather fgets. gets leaves you vulnerable to buffer-overflow, which is a common hacker exploit. Though if you're reading these words in from a disk file, then you'd be using fgets anyway.

    Are you familiar with the string functions which are prototyped in string.h? strlen() returns the length of the string minus the null-terminator. Just remember that the string's indices start at zero.

    You might also at some time want to become famliar with the functions in ctype.h. For example, isalpha() returns true if the character is a letter A-Z, a-z. isspace() tests for it being a "space", which includes space, newline, tab, etc. Read their documentation for more information (eg, Google on man page isspace).

    And from that weird redefinition of str, I assume that you are using C99 or something equally out-of-the-ordinary. When you do that, please at least have the common courtesy to inform us of that fact. If we were to try to compile your C99 program assuming it to be C, then it would refuse to compile and we'd come back reporting a lot of errors. There is no need for you to generate that kind of confusion. Be up-front about what you're using.
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,891
    Rep Power
    481
    strlen
    Find out which functions are defined in string.h.

    Meanwhile, I'll look up strfry.

    Comments on this post

    • dwise1_aol agrees : Yes, strfry is not only fast, but the results are always quite good. {grin}
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo