Hi, I am trying to do this practice example in C. WOuld someone please be able t
ID: 3714706 • Letter: H
Question
Hi, I am trying to do this practice example in C. WOuld someone please be able to first, descrive the soltuion in psuedo code, then complete in C in the simplest way?
Thanks a lot!
Consider a Carray holding words, such as argv which we are used to receiving in C's main function. Write a function that allocates memory for a single C string to hold all the words and make them into a proper sentence, starting with a capital, separated by spaces, and ending in a period. Do this without the C string library (only #include's of stdio and stdlib shown in the code below are allowed, no others). Ensure you handle all aspects of properly formed "C strings" both in the inputs and in the output. You can assume the first letter of the 0th argument is a small letter (between 'a' and 'z').Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
char* make_sentence(int num_words,char *word_array[])
{
int i,j;
int c,pp,sum=0;
i=0;
//finding string starting letter and string with period
while(i<num_words)
{
j=0;
// printf("Hello ");
while(word_array[i][j]!='')//finding length of the each word
{
// printf("Hello ");
if(j==0)//finding string starting with capital letter
{
int n = (int)word_array[i][j];
if(n>=65 && n<=91)
c=i;
}
j++;
}
//finding string ending with period
if(word_array[i][j-1]=='.')
pp=i;
sum = sum+j+1;
i++;
}
//printf("Hello ");
//dynamically creating memory....
char *p = (char *)malloc(sizeof(char)*sum);
i=0;
j=0;
//making string....
while(word_array[c][i]!='')
{
p[i]=word_array[c][i];
i++;
}
p[i]=' ';
i++;
int k=0;
while(k<num_words)
{
if(k!=c && k!=pp)
{
j=0;
while(word_array[k][j]!='')//finding length of the each word
{
p[i] = word_array[k][j];
i++;
j++;
}
p[i]=' ';
i++;
}
k++;
}
j=0;
while(word_array[pp][j]!='')
{
p[i]=word_array[pp][j];
i++;
j++;
}
p[i]=' ';
while(i<sum)
{
p[i]='';
i++;
}
return p;
}
int main(int argc,char *argv[])
{
printf("After combining, my arguments were %s ",make_sentence(argc,argv));
return 0;
}
/*
//change this main method as per ur code
//i have used it for testing....
int main()
{
//char a[3][5]={{'A','b','c','d','e'},{'s','u','r','y','a'},{'h','a','i','i','.'}};
char *a[5]={"hello","Surya","haii."};
int i=0;
char *p = make_sentence(3,a);
while(p[i]!='')
{printf("%c",p[i]);
i++;
}
return 0;
}*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.