Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1) Write a C program or edit my code which reads the standard input and prints t

ID: 3775063 • Letter: 1

Question

1) Write a C program or edit my code which reads the standard input and prints the longest word found.

A word is considered to be any sequence of letters; and we also allow characters minus (-) and aapostrophe (’ also known as single quote) to be a part of the word if surrounded by letters.

You can assume that the input text will not have a word longer than 1000 characters.

Sample Input

Sample Output

------------------------------------------------------------------------------

Edit the code below without using gets() because it dosent work in my university terminal also don't use <iostream.h> will also not work with me my terminal C98. Thank you : )

#include <stdio.h>
    #include <string.h>
    int main()
    {
        char strin[1000], word[1000], larg[1000];
        int i = 0, j = 0, flag = 0;
        printf("Enter The String: ");
        gets(strin);
        for (i = 0; i < strlen(strin); i++)
        {
            while (i < strlen(strin) && strin[i]!=32 && strin[i]!=0)
            {
                word[j++] = strin[i++];
            }
            if (j != 0)
            {
                word[j] = '';
                if (!flag)
                {
                    flag = !flag;
                    strcpy(larg, word);
                }
                if (strlen(word) > strlen(larg))
                {
                    strcpy(larg, word);
                }
                j = 0;
            }
        }
        printf("The Largest Word In The Entered String Is '%s' . ", larg);
        return 0;
    }

Explanation / Answer

Solution:

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

void main()
{
int i, j, count = 0, pos, flag = 0;
char s1[100], s2[10], s3[100];
char *ptr1, *ptr2, *ptr3;
printf(" enter the String:");
scanf(" %[^ ]s", s1);
printf(" enter the string to be inserted:");
scanf(" %[^ ]s", s2);
printf(" enter the position you like to insert:");
scanf("%d", &pos);
   ptr1 = s1;
ptr3 = s3;
   for (i = 0, j = 0;*ptr1 != ''; ptr1++, i++, j++, ptr3++)
{
s3[j] = s1[i];
if (*ptr1 == ' ' && flag != 1)
++count;
if (flag != 1 && count == pos - 1)
{
flag = 1;
for(ptr2 = s2;*ptr2 != ''; ptr2++)
{
s3[++j] = *ptr2;
ptr3++;
}
s3[++j] = ' ';
ptr3++;
}
}
s3[j] = '';
printf(" The Largest Word In The Entered String Is %s ", s3);
}