How to check first letter of one string with last letter of another string inside of same char array












-1














How can I complete the function canArrangeWords() ?



Question : Given a set of words check if we can arrange them in a list such that the last letter of any word and first letter of another word are same. The input function canArrangeWords shall contain an integer num and array of words arr. num denotes the number of word in the list (1<=num<=100). arr shall contain words consisting of lower case letters between 'a' - 'z' only . return 1 if words can be arranged in that fashion and -1 if cannot.



Input : 4  pot ten nice eye
output : 1
input : 3 fox owl pond
output: -1


Please help me complete this program .
**



#include<stdio.h>
#include<string.h>
int canArrangewords(int,char [100][100]);


void main(){
int n ,count=0 , i ;
char arrayS[100][100];
scanf("%d",&n);
for (i = 0; i < n; ++i)
{
scanf("%s",arrayS[i]);

}

for(i=0;i<n;i++)
{
printf("%s",arrayS[i]);
printf("n");

}
printf("%cn",arrayS[2][4]);

canArrangewords(n , arrayS);

}

int canArrangewords(int n,char arrayS[100][100]){


int i , j ;
for ( i = 0; i < n; i++)
{
for ( j = i+1 ; j < strlen(arrayS[j+1]); i++)
{
int flag = strlen(arrayS[j+1]) - 1;
int temp = strcmp(arrayS[i][0],arrayS[j][flag]);



}

}


}



}









share|improve this question





























    -1














    How can I complete the function canArrangeWords() ?



    Question : Given a set of words check if we can arrange them in a list such that the last letter of any word and first letter of another word are same. The input function canArrangeWords shall contain an integer num and array of words arr. num denotes the number of word in the list (1<=num<=100). arr shall contain words consisting of lower case letters between 'a' - 'z' only . return 1 if words can be arranged in that fashion and -1 if cannot.



    Input : 4  pot ten nice eye
    output : 1
    input : 3 fox owl pond
    output: -1


    Please help me complete this program .
    **



    #include<stdio.h>
    #include<string.h>
    int canArrangewords(int,char [100][100]);


    void main(){
    int n ,count=0 , i ;
    char arrayS[100][100];
    scanf("%d",&n);
    for (i = 0; i < n; ++i)
    {
    scanf("%s",arrayS[i]);

    }

    for(i=0;i<n;i++)
    {
    printf("%s",arrayS[i]);
    printf("n");

    }
    printf("%cn",arrayS[2][4]);

    canArrangewords(n , arrayS);

    }

    int canArrangewords(int n,char arrayS[100][100]){


    int i , j ;
    for ( i = 0; i < n; i++)
    {
    for ( j = i+1 ; j < strlen(arrayS[j+1]); i++)
    {
    int flag = strlen(arrayS[j+1]) - 1;
    int temp = strcmp(arrayS[i][0],arrayS[j][flag]);



    }

    }


    }



    }









    share|improve this question



























      -1












      -1








      -1







      How can I complete the function canArrangeWords() ?



      Question : Given a set of words check if we can arrange them in a list such that the last letter of any word and first letter of another word are same. The input function canArrangeWords shall contain an integer num and array of words arr. num denotes the number of word in the list (1<=num<=100). arr shall contain words consisting of lower case letters between 'a' - 'z' only . return 1 if words can be arranged in that fashion and -1 if cannot.



      Input : 4  pot ten nice eye
      output : 1
      input : 3 fox owl pond
      output: -1


      Please help me complete this program .
      **



      #include<stdio.h>
      #include<string.h>
      int canArrangewords(int,char [100][100]);


      void main(){
      int n ,count=0 , i ;
      char arrayS[100][100];
      scanf("%d",&n);
      for (i = 0; i < n; ++i)
      {
      scanf("%s",arrayS[i]);

      }

      for(i=0;i<n;i++)
      {
      printf("%s",arrayS[i]);
      printf("n");

      }
      printf("%cn",arrayS[2][4]);

      canArrangewords(n , arrayS);

      }

      int canArrangewords(int n,char arrayS[100][100]){


      int i , j ;
      for ( i = 0; i < n; i++)
      {
      for ( j = i+1 ; j < strlen(arrayS[j+1]); i++)
      {
      int flag = strlen(arrayS[j+1]) - 1;
      int temp = strcmp(arrayS[i][0],arrayS[j][flag]);



      }

      }


      }



      }









      share|improve this question















      How can I complete the function canArrangeWords() ?



      Question : Given a set of words check if we can arrange them in a list such that the last letter of any word and first letter of another word are same. The input function canArrangeWords shall contain an integer num and array of words arr. num denotes the number of word in the list (1<=num<=100). arr shall contain words consisting of lower case letters between 'a' - 'z' only . return 1 if words can be arranged in that fashion and -1 if cannot.



      Input : 4  pot ten nice eye
      output : 1
      input : 3 fox owl pond
      output: -1


      Please help me complete this program .
      **



      #include<stdio.h>
      #include<string.h>
      int canArrangewords(int,char [100][100]);


      void main(){
      int n ,count=0 , i ;
      char arrayS[100][100];
      scanf("%d",&n);
      for (i = 0; i < n; ++i)
      {
      scanf("%s",arrayS[i]);

      }

      for(i=0;i<n;i++)
      {
      printf("%s",arrayS[i]);
      printf("n");

      }
      printf("%cn",arrayS[2][4]);

      canArrangewords(n , arrayS);

      }

      int canArrangewords(int n,char arrayS[100][100]){


      int i , j ;
      for ( i = 0; i < n; i++)
      {
      for ( j = i+1 ; j < strlen(arrayS[j+1]); i++)
      {
      int flag = strlen(arrayS[j+1]) - 1;
      int temp = strcmp(arrayS[i][0],arrayS[j][flag]);



      }

      }


      }



      }






      c arrays string function






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 16:32









      Tim Randall

      1,9221119




      1,9221119










      asked Nov 12 at 15:47









      samriddha

      112




      112
























          2 Answers
          2






          active

          oldest

          votes


















          0














          Well, first of all think of the way you can reach that answer.



          If you only need to know if they can or can not be arranged and you do not have to do so your self you can use an empty array of int array[26] for each letter a-z.



          The rule is that from all the first and last letters for all the words only two MAY appear an odd amount of times - the first letter of first word in list and the last letter in the last word in the list, the rest MUST appear an even amount of times. I would add a check to make sure the letters are lowercase as well. good luck!



          #include <stdio.h>
          #include <stdlib.h>
          #include <string.h>

          #define MINASCII 97
          #define LETTERS 26
          void UpdateArray(char letter, int* arr)
          {

          if(arr[letter - MINASCII] == 0)
          {
          ++arr[letter - MINASCII];
          }
          else
          {
          --arr[letter - MINASCII];/*for each second time same letter is seen reduce back to zero */
          }
          }
          int canArrangewords(int wordNum, char* wordArr)
          {
          int arr[LETTERS] = {0};
          int i = 0;
          int count = 0 ;
          char first;
          char last;
          char* string;
          for (i= 0; i< wordNum; ++i)
          {
          string = wordArr[i];
          first = string[0];
          last = string[strlen(string)-1];
          UpdateArray(first, &arr[0]);
          UpdateArray(last, &arr[0]);
          }

          for(i = 0; i< LETTERS; ++i)
          {
          count+=arr[i];
          }
          if(count == 2 || count == 0)/*either once each or twice -see word1 example in main*/
          {
          return 1;
          }
          return -1;
          }


          int main()
          {
          int i = 0;
          char* words = {"pot", "ten", "nice", "eye"};
          char* words1 = {"pot", "ten", "nip"};
          char* words2 = {"fox", "owl", "pond"};
          i = canArrangewords(4,words);
          printf("%dn", i);
          i = canArrangewords(3,words1);
          printf("%dn", i);
          i = canArrangewords(3,words2);
          printf("%dn", i);
          return 0;
          }





          share|improve this answer





















          • Thank you for your answer . Can you please explain this part of your code . I'm not able to understand clearly what this function is doing. if(arr[letter - MINASCII] == 0) { ++arr[letter - MINASCII]; } else { --arr[letter - MINASCII]; }
            – samriddha
            Nov 13 at 12:09










          • this function is set to tell us if a certain letter was seen an odd amount of times or not. for example- for the letter 'a'. the ascii val is 97 so we go to the array[in place[97-97] which is arr[0](for b it will be arr[98-97]==arr[1]). Now- for the first time we get a- the array will be updated to 1. if the letter appears again- an even amount of times we reset arr[0] to zero. if we get a 3 times arr[0] will be 1 and for 4 times will be zero again.
            – H.cohen
            Nov 13 at 12:24



















          0














          Change your array of words into an array of pointers to words. Then you can simply exchange the pointers.



          To speed things up, instead of a pointer to a word, have it point to a structure:



          struct WORD {
          char *firstchar; // begin of word
          char *lastchar; // last char of word
          } *words[100]; // array of 100 pointers to words


          To read the words:



          char buf[100];
          for (i = 0; i < n; ++i)
          {
          scanf("%s",buf);
          int len= strlen(buf);
          words[i]= malloc(sizeof(struct WORDS));
          words[i]->firstchar= malloc(len+1);
          strcpy(words[i]->firstchar, buf);
          words[i]->lastchar= words[i]->firstchar + len-1;
          }


          Now compare and sort:



                  if (*words[i]->lastchar == *words[j]->firstchar) {
          struct WORDS *tmp= words[i+1];
          words[i+1]= words[j];
          words[j]= tmp;
          }


          Do this in a loop, a kind of bubble sort. I leave that to you.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265589%2fhow-to-check-first-letter-of-one-string-with-last-letter-of-another-string-insid%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Well, first of all think of the way you can reach that answer.



            If you only need to know if they can or can not be arranged and you do not have to do so your self you can use an empty array of int array[26] for each letter a-z.



            The rule is that from all the first and last letters for all the words only two MAY appear an odd amount of times - the first letter of first word in list and the last letter in the last word in the list, the rest MUST appear an even amount of times. I would add a check to make sure the letters are lowercase as well. good luck!



            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>

            #define MINASCII 97
            #define LETTERS 26
            void UpdateArray(char letter, int* arr)
            {

            if(arr[letter - MINASCII] == 0)
            {
            ++arr[letter - MINASCII];
            }
            else
            {
            --arr[letter - MINASCII];/*for each second time same letter is seen reduce back to zero */
            }
            }
            int canArrangewords(int wordNum, char* wordArr)
            {
            int arr[LETTERS] = {0};
            int i = 0;
            int count = 0 ;
            char first;
            char last;
            char* string;
            for (i= 0; i< wordNum; ++i)
            {
            string = wordArr[i];
            first = string[0];
            last = string[strlen(string)-1];
            UpdateArray(first, &arr[0]);
            UpdateArray(last, &arr[0]);
            }

            for(i = 0; i< LETTERS; ++i)
            {
            count+=arr[i];
            }
            if(count == 2 || count == 0)/*either once each or twice -see word1 example in main*/
            {
            return 1;
            }
            return -1;
            }


            int main()
            {
            int i = 0;
            char* words = {"pot", "ten", "nice", "eye"};
            char* words1 = {"pot", "ten", "nip"};
            char* words2 = {"fox", "owl", "pond"};
            i = canArrangewords(4,words);
            printf("%dn", i);
            i = canArrangewords(3,words1);
            printf("%dn", i);
            i = canArrangewords(3,words2);
            printf("%dn", i);
            return 0;
            }





            share|improve this answer





















            • Thank you for your answer . Can you please explain this part of your code . I'm not able to understand clearly what this function is doing. if(arr[letter - MINASCII] == 0) { ++arr[letter - MINASCII]; } else { --arr[letter - MINASCII]; }
              – samriddha
              Nov 13 at 12:09










            • this function is set to tell us if a certain letter was seen an odd amount of times or not. for example- for the letter 'a'. the ascii val is 97 so we go to the array[in place[97-97] which is arr[0](for b it will be arr[98-97]==arr[1]). Now- for the first time we get a- the array will be updated to 1. if the letter appears again- an even amount of times we reset arr[0] to zero. if we get a 3 times arr[0] will be 1 and for 4 times will be zero again.
              – H.cohen
              Nov 13 at 12:24
















            0














            Well, first of all think of the way you can reach that answer.



            If you only need to know if they can or can not be arranged and you do not have to do so your self you can use an empty array of int array[26] for each letter a-z.



            The rule is that from all the first and last letters for all the words only two MAY appear an odd amount of times - the first letter of first word in list and the last letter in the last word in the list, the rest MUST appear an even amount of times. I would add a check to make sure the letters are lowercase as well. good luck!



            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>

            #define MINASCII 97
            #define LETTERS 26
            void UpdateArray(char letter, int* arr)
            {

            if(arr[letter - MINASCII] == 0)
            {
            ++arr[letter - MINASCII];
            }
            else
            {
            --arr[letter - MINASCII];/*for each second time same letter is seen reduce back to zero */
            }
            }
            int canArrangewords(int wordNum, char* wordArr)
            {
            int arr[LETTERS] = {0};
            int i = 0;
            int count = 0 ;
            char first;
            char last;
            char* string;
            for (i= 0; i< wordNum; ++i)
            {
            string = wordArr[i];
            first = string[0];
            last = string[strlen(string)-1];
            UpdateArray(first, &arr[0]);
            UpdateArray(last, &arr[0]);
            }

            for(i = 0; i< LETTERS; ++i)
            {
            count+=arr[i];
            }
            if(count == 2 || count == 0)/*either once each or twice -see word1 example in main*/
            {
            return 1;
            }
            return -1;
            }


            int main()
            {
            int i = 0;
            char* words = {"pot", "ten", "nice", "eye"};
            char* words1 = {"pot", "ten", "nip"};
            char* words2 = {"fox", "owl", "pond"};
            i = canArrangewords(4,words);
            printf("%dn", i);
            i = canArrangewords(3,words1);
            printf("%dn", i);
            i = canArrangewords(3,words2);
            printf("%dn", i);
            return 0;
            }





            share|improve this answer





















            • Thank you for your answer . Can you please explain this part of your code . I'm not able to understand clearly what this function is doing. if(arr[letter - MINASCII] == 0) { ++arr[letter - MINASCII]; } else { --arr[letter - MINASCII]; }
              – samriddha
              Nov 13 at 12:09










            • this function is set to tell us if a certain letter was seen an odd amount of times or not. for example- for the letter 'a'. the ascii val is 97 so we go to the array[in place[97-97] which is arr[0](for b it will be arr[98-97]==arr[1]). Now- for the first time we get a- the array will be updated to 1. if the letter appears again- an even amount of times we reset arr[0] to zero. if we get a 3 times arr[0] will be 1 and for 4 times will be zero again.
              – H.cohen
              Nov 13 at 12:24














            0












            0








            0






            Well, first of all think of the way you can reach that answer.



            If you only need to know if they can or can not be arranged and you do not have to do so your self you can use an empty array of int array[26] for each letter a-z.



            The rule is that from all the first and last letters for all the words only two MAY appear an odd amount of times - the first letter of first word in list and the last letter in the last word in the list, the rest MUST appear an even amount of times. I would add a check to make sure the letters are lowercase as well. good luck!



            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>

            #define MINASCII 97
            #define LETTERS 26
            void UpdateArray(char letter, int* arr)
            {

            if(arr[letter - MINASCII] == 0)
            {
            ++arr[letter - MINASCII];
            }
            else
            {
            --arr[letter - MINASCII];/*for each second time same letter is seen reduce back to zero */
            }
            }
            int canArrangewords(int wordNum, char* wordArr)
            {
            int arr[LETTERS] = {0};
            int i = 0;
            int count = 0 ;
            char first;
            char last;
            char* string;
            for (i= 0; i< wordNum; ++i)
            {
            string = wordArr[i];
            first = string[0];
            last = string[strlen(string)-1];
            UpdateArray(first, &arr[0]);
            UpdateArray(last, &arr[0]);
            }

            for(i = 0; i< LETTERS; ++i)
            {
            count+=arr[i];
            }
            if(count == 2 || count == 0)/*either once each or twice -see word1 example in main*/
            {
            return 1;
            }
            return -1;
            }


            int main()
            {
            int i = 0;
            char* words = {"pot", "ten", "nice", "eye"};
            char* words1 = {"pot", "ten", "nip"};
            char* words2 = {"fox", "owl", "pond"};
            i = canArrangewords(4,words);
            printf("%dn", i);
            i = canArrangewords(3,words1);
            printf("%dn", i);
            i = canArrangewords(3,words2);
            printf("%dn", i);
            return 0;
            }





            share|improve this answer












            Well, first of all think of the way you can reach that answer.



            If you only need to know if they can or can not be arranged and you do not have to do so your self you can use an empty array of int array[26] for each letter a-z.



            The rule is that from all the first and last letters for all the words only two MAY appear an odd amount of times - the first letter of first word in list and the last letter in the last word in the list, the rest MUST appear an even amount of times. I would add a check to make sure the letters are lowercase as well. good luck!



            #include <stdio.h>
            #include <stdlib.h>
            #include <string.h>

            #define MINASCII 97
            #define LETTERS 26
            void UpdateArray(char letter, int* arr)
            {

            if(arr[letter - MINASCII] == 0)
            {
            ++arr[letter - MINASCII];
            }
            else
            {
            --arr[letter - MINASCII];/*for each second time same letter is seen reduce back to zero */
            }
            }
            int canArrangewords(int wordNum, char* wordArr)
            {
            int arr[LETTERS] = {0};
            int i = 0;
            int count = 0 ;
            char first;
            char last;
            char* string;
            for (i= 0; i< wordNum; ++i)
            {
            string = wordArr[i];
            first = string[0];
            last = string[strlen(string)-1];
            UpdateArray(first, &arr[0]);
            UpdateArray(last, &arr[0]);
            }

            for(i = 0; i< LETTERS; ++i)
            {
            count+=arr[i];
            }
            if(count == 2 || count == 0)/*either once each or twice -see word1 example in main*/
            {
            return 1;
            }
            return -1;
            }


            int main()
            {
            int i = 0;
            char* words = {"pot", "ten", "nice", "eye"};
            char* words1 = {"pot", "ten", "nip"};
            char* words2 = {"fox", "owl", "pond"};
            i = canArrangewords(4,words);
            printf("%dn", i);
            i = canArrangewords(3,words1);
            printf("%dn", i);
            i = canArrangewords(3,words2);
            printf("%dn", i);
            return 0;
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 12 at 17:06









            H.cohen

            2125




            2125












            • Thank you for your answer . Can you please explain this part of your code . I'm not able to understand clearly what this function is doing. if(arr[letter - MINASCII] == 0) { ++arr[letter - MINASCII]; } else { --arr[letter - MINASCII]; }
              – samriddha
              Nov 13 at 12:09










            • this function is set to tell us if a certain letter was seen an odd amount of times or not. for example- for the letter 'a'. the ascii val is 97 so we go to the array[in place[97-97] which is arr[0](for b it will be arr[98-97]==arr[1]). Now- for the first time we get a- the array will be updated to 1. if the letter appears again- an even amount of times we reset arr[0] to zero. if we get a 3 times arr[0] will be 1 and for 4 times will be zero again.
              – H.cohen
              Nov 13 at 12:24


















            • Thank you for your answer . Can you please explain this part of your code . I'm not able to understand clearly what this function is doing. if(arr[letter - MINASCII] == 0) { ++arr[letter - MINASCII]; } else { --arr[letter - MINASCII]; }
              – samriddha
              Nov 13 at 12:09










            • this function is set to tell us if a certain letter was seen an odd amount of times or not. for example- for the letter 'a'. the ascii val is 97 so we go to the array[in place[97-97] which is arr[0](for b it will be arr[98-97]==arr[1]). Now- for the first time we get a- the array will be updated to 1. if the letter appears again- an even amount of times we reset arr[0] to zero. if we get a 3 times arr[0] will be 1 and for 4 times will be zero again.
              – H.cohen
              Nov 13 at 12:24
















            Thank you for your answer . Can you please explain this part of your code . I'm not able to understand clearly what this function is doing. if(arr[letter - MINASCII] == 0) { ++arr[letter - MINASCII]; } else { --arr[letter - MINASCII]; }
            – samriddha
            Nov 13 at 12:09




            Thank you for your answer . Can you please explain this part of your code . I'm not able to understand clearly what this function is doing. if(arr[letter - MINASCII] == 0) { ++arr[letter - MINASCII]; } else { --arr[letter - MINASCII]; }
            – samriddha
            Nov 13 at 12:09












            this function is set to tell us if a certain letter was seen an odd amount of times or not. for example- for the letter 'a'. the ascii val is 97 so we go to the array[in place[97-97] which is arr[0](for b it will be arr[98-97]==arr[1]). Now- for the first time we get a- the array will be updated to 1. if the letter appears again- an even amount of times we reset arr[0] to zero. if we get a 3 times arr[0] will be 1 and for 4 times will be zero again.
            – H.cohen
            Nov 13 at 12:24




            this function is set to tell us if a certain letter was seen an odd amount of times or not. for example- for the letter 'a'. the ascii val is 97 so we go to the array[in place[97-97] which is arr[0](for b it will be arr[98-97]==arr[1]). Now- for the first time we get a- the array will be updated to 1. if the letter appears again- an even amount of times we reset arr[0] to zero. if we get a 3 times arr[0] will be 1 and for 4 times will be zero again.
            – H.cohen
            Nov 13 at 12:24













            0














            Change your array of words into an array of pointers to words. Then you can simply exchange the pointers.



            To speed things up, instead of a pointer to a word, have it point to a structure:



            struct WORD {
            char *firstchar; // begin of word
            char *lastchar; // last char of word
            } *words[100]; // array of 100 pointers to words


            To read the words:



            char buf[100];
            for (i = 0; i < n; ++i)
            {
            scanf("%s",buf);
            int len= strlen(buf);
            words[i]= malloc(sizeof(struct WORDS));
            words[i]->firstchar= malloc(len+1);
            strcpy(words[i]->firstchar, buf);
            words[i]->lastchar= words[i]->firstchar + len-1;
            }


            Now compare and sort:



                    if (*words[i]->lastchar == *words[j]->firstchar) {
            struct WORDS *tmp= words[i+1];
            words[i+1]= words[j];
            words[j]= tmp;
            }


            Do this in a loop, a kind of bubble sort. I leave that to you.






            share|improve this answer


























              0














              Change your array of words into an array of pointers to words. Then you can simply exchange the pointers.



              To speed things up, instead of a pointer to a word, have it point to a structure:



              struct WORD {
              char *firstchar; // begin of word
              char *lastchar; // last char of word
              } *words[100]; // array of 100 pointers to words


              To read the words:



              char buf[100];
              for (i = 0; i < n; ++i)
              {
              scanf("%s",buf);
              int len= strlen(buf);
              words[i]= malloc(sizeof(struct WORDS));
              words[i]->firstchar= malloc(len+1);
              strcpy(words[i]->firstchar, buf);
              words[i]->lastchar= words[i]->firstchar + len-1;
              }


              Now compare and sort:



                      if (*words[i]->lastchar == *words[j]->firstchar) {
              struct WORDS *tmp= words[i+1];
              words[i+1]= words[j];
              words[j]= tmp;
              }


              Do this in a loop, a kind of bubble sort. I leave that to you.






              share|improve this answer
























                0












                0








                0






                Change your array of words into an array of pointers to words. Then you can simply exchange the pointers.



                To speed things up, instead of a pointer to a word, have it point to a structure:



                struct WORD {
                char *firstchar; // begin of word
                char *lastchar; // last char of word
                } *words[100]; // array of 100 pointers to words


                To read the words:



                char buf[100];
                for (i = 0; i < n; ++i)
                {
                scanf("%s",buf);
                int len= strlen(buf);
                words[i]= malloc(sizeof(struct WORDS));
                words[i]->firstchar= malloc(len+1);
                strcpy(words[i]->firstchar, buf);
                words[i]->lastchar= words[i]->firstchar + len-1;
                }


                Now compare and sort:



                        if (*words[i]->lastchar == *words[j]->firstchar) {
                struct WORDS *tmp= words[i+1];
                words[i+1]= words[j];
                words[j]= tmp;
                }


                Do this in a loop, a kind of bubble sort. I leave that to you.






                share|improve this answer












                Change your array of words into an array of pointers to words. Then you can simply exchange the pointers.



                To speed things up, instead of a pointer to a word, have it point to a structure:



                struct WORD {
                char *firstchar; // begin of word
                char *lastchar; // last char of word
                } *words[100]; // array of 100 pointers to words


                To read the words:



                char buf[100];
                for (i = 0; i < n; ++i)
                {
                scanf("%s",buf);
                int len= strlen(buf);
                words[i]= malloc(sizeof(struct WORDS));
                words[i]->firstchar= malloc(len+1);
                strcpy(words[i]->firstchar, buf);
                words[i]->lastchar= words[i]->firstchar + len-1;
                }


                Now compare and sort:



                        if (*words[i]->lastchar == *words[j]->firstchar) {
                struct WORDS *tmp= words[i+1];
                words[i+1]= words[j];
                words[j]= tmp;
                }


                Do this in a loop, a kind of bubble sort. I leave that to you.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 12 at 17:00









                Paul Ogilvie

                17.3k11234




                17.3k11234






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53265589%2fhow-to-check-first-letter-of-one-string-with-last-letter-of-another-string-insid%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python