Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C string program

Tags:

c

string

I have been given a task at school to write a program that

  • Reads three strings
  • Stores the third string in dynamically allocated memory
  • Print out the last 4 letters of the first word alphabetically.

Here is the program I have so far. The strings are all stored in different variables, making them hard to sort. If anyone could give me a hand and help me finish this program, I would be very grateful.

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

int main()
{
   char word1[101];
   char word2[101];
   char* word3;
   char buffer[101];
   scanf("%s", word1);
   scanf("%s", word2);
   scanf("%s", buffer);
   word3 = (char *) malloc(strlen(buffer)+1);
   strcpy(word3, buffer);

   return 0;
}
like image 378
mrblippy Avatar asked Jun 07 '10 04:06

mrblippy


1 Answers

You can use the strcmp() function to compare the strings.

Also, don't forget to clean up the memory pointed to by word3 using the free() function before you are finished.

like image 148
VeeArr Avatar answered Oct 08 '22 21:10

VeeArr