The function/method findNextWordPosition accepts an argument str. The string str contains multiple words.
The function/method findNextWordPosition must print the output based on the following conditions.
– For each word in the given string, the function must print the position of the next word with the same last character.
– Consider the words in circular fashion when finding the next word with the same last character.
Your task is to implement the function findNextWordPosition so that the program runs successfully.
IMPORTANT: Do not write the main() function as it is already defined.
Example Input/Output 1:
Input:
pen table desk admin junction rock
Output:
4 2 6 5 1 3
Explanation:
pen -> admin (4th word)
table -> table (2nd word)
desk -> rock (6th word)
admin -> junction (5th word)
junction -> pen (1st word)
rock -> desk (3rd word)
Hence the output is
4 2 6 5 1 3
Example Input/Output 2:
Input:
one two three four five
Output:
3 2 5 4 1
#include <stdio.h>
#include <stdlib.h>
void findNextWordPosition(char *str)
{
char matrix[1001][1001];
int i=0,j=0,k=0;
while(str[j]!=' ')
{
if(str[j]==' ')
{
i++;
k=0;
}
else
matrix[i][k++]=str[j];
j++;
}
i++;
for(int l=0;l<i;l++)
{
int temp=l+1;
if(temp==i)
temp=0;
while (1)
{
int end=strlen(matrix[l]);
int e=strlen(matrix[temp]);
if(matrix[l][end-1]==matrix[temp][e-1])
{
printf("%d ",temp+1);
break;
}
temp++;
if(temp==i)
temp=0;
}
}
}
int main()
{
char str[101];
scanf("%[^nr]", str);
findNextWordPosition(str);
return 0;
}