3. String Search Input one line of sentence, and a string. Then: a) Write a func
ID: 3586736 • Letter: 3
Question
3. String Search Input one line of sentence, and a string. Then: a) Write a function to extract all words from that sentence into a global 2-dimensional char arrays and their start position in the sentence. Call this function in your main function and then print the results b) Write another function to decide whether a word contains a certain string in a case- insensitive manner. Call this function in your main function for all the words. Then output all words that does contain the string, together with their start and end position at the original sentence c) Make use of the functions you write in your main function Example & Output Format Requirements our sentence Are you high? Remember.you sweared to live a HEALTHIER life our string to be searched: ords found Hi Are you high Remember you sweared to live a HEALTHIER life earch result: Hi 0 1 igh 12 15 EAL THIER 50 58 Press any key to continue Assumptions Punctuations only include: 2 . ii. No redundant space at the beginning and the end of the sentence iii.Words are separated by either one single space or one punctuation one single space iv. The number of words is no more than 1000, and the length of each word is no more than 15 Other Requirements Do not include any header files other thanExplanation / Answer
/**************************************************************************************/
#include <stdio.h>
int StringToArray(void);
int CheckLine(char * line);
int CheckWord(void);
int charNumber(int i,int j);
char lineArray[1000][15];
int MyStrlen(char * word);
int countWords =0 ;
void main(void)
{
int i=0;
int ret = 0;
ret = StringToArray();
if(ret == -1)
return ;
printf("Line in Array: ");
for(i=0 ; i <= countWords ;i++)
printf("%s ",lineArray[i]);
printf(" ");
if(CheckWord() == 1)
{
printf("No currectiong!!! ");
}
}
int CheckLine(char * line)
{
int i =0;
for(i=0; line[i];i++)
{
if((line[i]>= 'A' && line[i] <= 'Z') || (line[i]>= 'a' && line[i] <= 'z') || (line[i]>= '0' && line[i] <= '9') || (line[i] == ',') || (line[i] == '?') || (line[i] == '!') || (line[i] == ' ')) // Check all conditions
{
if((i == 0) && (line[i] == ' '))
return -1;
continue;
}
else
return line[i]; // return unknown character
}
return 0;
}
int StringToArray(void)
{
int i =0 , j =0 , k=0;
int ret =0;
char line[1000 * 15];
printf("Please enter line ");
scanf(" %[^ ]s",line);
ret = CheckLine(line);
if(ret != 0)
{
if(ret == -1)
printf("Do not give space in start of the line ");
if(ret > 0)
printf(" %c is not acceptable ",ret);
return -1;
}
for(i = 0,k=0 ; line[i] ; i++)
{
if(line[i] == ' ' || (line[i] == ',') || (line[i] == '?') || (line[i] == '!'))
{
if((line[i+1] != ' ') || (line[i+1] != ' ') || (line[i+1] != '') || (line[i+1] != EOF))
{
lineArray[j][k] = '';
if(j >= 1000)
{
printf("words more then 1000 is not acceptable!!!! ");
return -1;
}
countWords = j++ + 1;
if( k > 15)
{
printf("word more then 15 latter is not acceptable!!!! ");
return -1;
}
k =0;
continue;
}
}
else
lineArray[j][k++] = line[i];
}
}
/**************************************************************************************/
/* B) and C)
*/
int CheckWord(void)
{
int i = 0,j = 0,flg = 1 ;
for(i = 0 ; i <= countWords;i++)
{
for(j=0;lineArray[i][j];j++)
{
if(flg == 1)
{
flg = 0;
if(lineArray[i][0] >= 'a' && lineArray[i][0] <= 'z')
printf("currection - %s %d %d ",lineArray[i],charNumber(i,0),charNumber(i,MyStrlen(lineArray[i])-1));
continue;
}
else if((lineArray[i][j] >= 'A' && lineArray[i][j] <= 'Z') && (i != 0))
{
printf("currection - %s %d %d ",lineArray[i],charNumber(i,0),charNumber(i,MyStrlen(lineArray[i])-1));
continue;
}
}
}
}
int charNumber(int i,int j)
{
int k , l , c =0;
for(k=0 ; k<=i;k++)
{
for(l=0;lineArray[k][l];l++)
{
c++;
if((lineArray[k][l] == lineArray[i][j]) && k==i)
{
return c;
}
}
}
return c;
}
int MyStrlen(char * word)
{
int i=0 ;
for(i=0;word[i];i++);
return i;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.