Thread: Need help with checking last 3 letters in string

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    2

    Need help with checking last 3 letters in string

    Hello everybody frist i need to say that am proud that i become member of this community i saw some posts from 2001 and its crazy 16 years :O GOOD JOB!!

    I have one question i have to do a task for my studies
    task is:
    input :
    ____________
    ____________
    ____________
    ____________

    four lines of strings ( song lyrics )

    and i need to check last three words of every line then check if frist line and thrid line have the same last three words? I know how to check last word (strlen(char name)-1)
    but how for three?

    I apologize if the questions are stupid and unclear but i am begginer!
    EDIT: One more thing when i check which is last how can i save it in variable?
    Last edited by nekinebitanlik; 11-10-2017 at 06:04 PM.

  2. #2
    Banned
    Join Date
    Aug 2017
    Posts
    861
    brush up on loops, and control values, keeping count of something, how to keep count of something, if statements. and maybe take a break even.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main (void)
    {
        
        char *letters = "11 22 32 4 5 6 7 8 hello";
        char saved_1_char[2];
    
        int len = strlen(letters);
        for ( ; len + 1 ; )  
        {
            printf("%c\n", letters[len]);
            if ( letters[--len] == 'e')
                 saved_1_char[0] = letters[len];
        }
       printf("saved a char %c\n", saved_1_char[0]);
            
        return 0;
     
    }
    posting your code on what you got so far is highly recommended.
    Last edited by userxbw; 11-10-2017 at 07:21 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by nekinebitanlik View Post
    and i need to check last three words of every line then check if frist line and thrid line have the same last three words?
    First you have to figure out how to isolate the last three words of a given line. Perhaps start at the end of a line and work your way backwards.

    Quote Originally Posted by nekinebitanlik View Post
    EDIT: One more thing when i check which is last how can i save it in variable?
    You can use strcpy to copy a string. If you understand arrays and '&', you can pick where you want the string copy to begin from.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You could save all of the positions in the string where there is a space. Assuming that there is only one space between words and that every line ends with \n, every space is where a word starts. Then, finding the third to last word and such in the text is trivial.

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    2
    Thanks for responding. I didnt post code cause i didnt write anything, i STILL dont have idea how to compare last three words in frist row with last three words in thrid row.. i know i am unclear so i will try to explain better:

    Console input and output is italic

    Enter four sentence:

    The condition of being abduc
    ted
    I love my mom
    I got connec
    ted
    I love my dad


    ///so frist and thrid sentence ending on ted

    Great job !
    Last edited by nekinebitanlik; 11-11-2017 at 11:46 AM.

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    88
    If this is helpful to you, this is a way to extract words:
    Code:
    #include <stdio.h>
     
    int main(void) {
    	char line[100];
    	fgets(line, 100, stdin);
    	/* or do this:
    	char line[100] = "this is a sentence"
    	*/
    	char words0[10][20];
    	char *token = strtok(line, " \n");
    	for (int i = 0; i < 10 && token != NULL; ++i) {
    		token = (NULL, " \n");
    	}
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help converting letters in a string
    By Jake Garbelotti in forum C Programming
    Replies: 12
    Last Post: 07-21-2012, 10:01 AM
  2. Replies: 7
    Last Post: 03-03-2010, 08:47 AM
  3. counting letters in string
    By CGbiginner in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 11:47 AM
  4. Replies: 2
    Last Post: 11-16-2006, 01:43 PM
  5. Checking for letters
    By brdgsa in forum C Programming
    Replies: 2
    Last Post: 12-05-2002, 10:20 AM

Tags for this Thread