Question

Lab Assighment-String Manipulation 1. Create an array of characters to hold a list of 10 words Read the words from the keyboa

in c

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//program1
#include <stdio.h>
#include <string.h>
#include<stdlib.h>

int main()
{
   //Declare Variables
   char array[10][30]; //2D array for storing strings
   int i, n;

   //Get the maximum number of strings
   printf("Enter number of words to input(<=10)\n");
   scanf("%d", &n);
   if(n>10){
   printf("invalid ");
   exit(0);
   }

   //Read the user input words
   printf("Enter the words one by one: \n");
   for(i=0; i< n ; i++) {
       scanf("%s",array[i]);
   }
   printf("\n");
   for(i=0; i< n ; i++) {
       printf("%s\n",array[i]);
   }

   printf("\n");
   for(i=0; i<n ; i=i+2) {
       //Concatenation function for two words
       strcat(array[i],array[i+1]);
   }
   for(i=0; i< n ; i=i+2) {
       //printing the word at index i
       printf("%s\n",array[i]);
   }
   printf("\n");
   printf("Enter the words again from the above one by one: \n");
   for(i=0; i< n ; i++) {
       scanf("%s",array[i]);
   }
   printf("\n");
   for(i=0; i<n ; i=i+3) {
       //Technique to concatenate three words
       strcat(array[i],array[i+1]);
       strcat(array[i],array[i+2]);
   }
   for(i=0; i< n ; i=i+3) {
       printf("%s\n",array[i]);
   }
   //Return to the system
   return 0;
}

//program 2
#include <stdio.h>
#include <string.h>

int main()
{

char array[10][8], tarray[10][8], temp[8];
int i, j, n;

printf("Enter the number of words n(<=5) \n");
scanf("%d", &n);
printf("Enter %d names\n", n);

for (i = 0; i < n; i++)
{
scanf("%s", array[i]);
strcpy(tarray[i], array[i]);
}
//using a temporary character array to sort the words

for (i = 0; i < n - 1 ; i++)
{
for (j = i + 1; j < n; j++)
{
if (strcmp(array[i], array[j]) > 0)
{
strcpy(temp, array[i]);
strcpy(array[i], array[j]);
strcpy(array[j], temp);
}
}
}

printf("\n");
printf("Sorted names are:\n");
    //printing the words in sorted order
for (i = 0; i < n; i++)
{
printf("%s\n",array[i]);
}
return 0;
}

g En (1DO) 1:57 AM zz.c (/Desktop) gedit Open Save 1//program 2 2 #1nclude <stdio.h #include <string.h> 5 int main) { char ar(1D0%) 154 AM guthula@guthula-Inspiron-15-3567: -/Desktop guthula@guthula- Inspiron-15-3567 : -$ cd Desktop 22. c quthula@qutg En 1.c (-/Desktop) - gedit (1DO) 4:05 AM Open Save 1//programl 2#1nctude<stdio.h 4#include<stdlib.h> 5 int main() //Declare//OUTPUT FOR PROGRAM1

(1D0%) :19 AM guthula@guthula-Inspiron-15-3567: -/Desktop guthula@guthula-Inspiron- 15 - 3567: -$ cd Desktop 1.c quthula@quth

Add a comment
Know the answer?
Add Answer to:
in c Lab Assighment-String Manipulation 1. Create an array of characters to hold a list of 10 words Read the words f...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT