You are to write a program that counts the number of letter pairs in a sentence
ID: 3803698 • Letter: Y
Question
You are to write a program that counts the number of letter pairs in a sentence generated by consecutively pairing the letters, excluding the space character. For example, given the sentence below, the consecutive pairs are shown. the rain in spain falls mainly on the plains th he ra ai in in sp pa ai in fa al ll ls ma ai in nl ly on th he pi la ai in ns For the above example, the count of pairs are: th 2 ai 4 pa 1 ll 1 n1 1 p1 1 he 2 in 5 fa 1 ls 1 1y 1 la 1 ra 1 sp 1 al 1 ma 1 on 1 ns 1 An example data file is shown below. 9 the rain in spain falls mainly on the plains 5 the country counsels the distance 5 the organization records the committee 6 the ashamed son revises the voice 6 the advertisement trades the responsible son 5 the danger publicizes the story 6 the shake regulates the fine credit 6 the list specifies the fresh powder 5 the shade reasons the night 6 the rain schedules the nauseating lift 5 the degree collates the grass For each sentence in the data file output the sentence and the number of pairs and the sentence in the same order as the data file. For example, the first few lines of output should appear as below. There is an unknown number of various size sentences. the rain in spain falls mainly on the plains th he ra ai in sp pa fa al ll ls ma nl ly on pi la ns 2 2 1 4 5 1 1 1 1 1 1 1 1 1 1 1 1 1 The country counsels the distance th he co ou un nt tr ry ns se el ls di is st ta an nc ce 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 The datafile is located at ~dmk0080/public/1040/labs/examl/wordpairs. Name your program letterpairs.e.Explanation / Answer
//main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Word
{
char *letters;
struct Word *next;
}word, permWord;
void openFile(FILE **inFile)
{
char input[50];
printf("What is the name of the input file? ");
scanf("%s", input);
*inFile = fopen(input, "r");
if (!(*inFile))
{
fputs("File error. ", stderr);
exit(1);
}
}
void readStream(FILE *inFile, word **head, word **train)
{
char inWord[20];
// printf("Reading stream. ");
// fscanf(inFile, " %s ", inWord);
// printf("%s ", inWord);
while (fscanf(inFile, " %s", inWord) != EOF)
{
// printf("%s ", inWord);
if (*head == NULL)
{
(*head) = (word *) malloc(sizeof(word));
(*head)->next = NULL;
(*train) = (*head);
}
else
{
(*train)->next = (word *) malloc(sizeof(word));
(*train) = (*train)->next;
(*train)->next = NULL;
}
(*train)->letters = (char *) malloc(sizeof(char) * strlen(inWord) + 1);
strcpy((*train)->letters, inWord);
printf("%s ", (*train)->letters);
}
}
void swap(char *word, int i, int j)
{
char temp = word[i];
word[i] = word[j];
word[j] = temp;
}
void permute(char *word, int start, int end)
{
int i;
if (start == end)
{
printf("%s ", word);
return;
}
permute(word, start + 1, end);
for (i = start; i < end; ++i)
{
if (word[start] == word[i])
continue;
swap(word, start, i);
permute(word, start + 1, end);
swap(word, start, i);
}
}
int main()
{
int i, j;
FILE *inFile;
word *wordHead = NULL;
word *wordTrain = NULL;
permWord *permHead = NULL;
permWord *permTrain = NULL;
char tempWord[20], tempLetter;
openFile(&inFile);
readStream(inFile, &wordHead, &wordTrain);
wordTrain = wordHead;
// while (wordTrain->next != NULL)
// {
int n = strlen(wordTrain->letters);
permute(wordTrain->letters, 0, n - 1);
wordTrain = wordTrain->next;
printf(" ");
// }
return 0;
}
=================================================================
wordpairs
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.