tags:

views:

233

answers:

5
+15  Q: 

C string program

Hi,

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.

Thanks

#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;
}
+2  A: 

Use strcmp to find the first word alphabetically.

#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);

   char* smallestword = word1;
   if (strcmp(smallestword,word2) > 0) // smallest is bigger than word2
     smallestword = word2;
   if (strcmp(smallestword,buffer) > 0) // smallest is bigger than buffer
     smallestword = buffer;

   word3 = (char *) malloc(strlen(smallestword)+1);
   strcpy(word3, buffer);
   return 0;
}
brickner
Thank you for your reply,i am aware that i have to do this. But i need help implementing it
mrblippy
The link he provided shows examples of how to use `strcmp()`.
VeeArr
Thank you so much, thats the code i had in mind, just wasnt quite sure how to write it
mrblippy
So do you know WHY it may or may not be correct code?
drachenstern
+3  A: 

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.

VeeArr
Thanks, how would i use strcmp on the three strings though? sorry i am new to this
mrblippy
A: 

Here's the program including code to get the substring of the last 4 characters of the smallest word. Also fixed a bug where word3 was always set to the last word input (buffer), not smallestword as intended.

#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);

char* smallestword = word1; 
if (strcmp(smallestword,word2) > 0) // smallest is bigger than word2
        smallestword = word2; 
if (strcmp(smallestword,buffer) > 0) // smallest is bigger than buffer
        smallestword = buffer;

word3 = (char *) malloc(strlen(smallestword)+1); 
strcpy(word3, smallestword);

int m = strlen( word3 ), n = m - 4;    // set offsets for substr
char* word3_substr = (char *) malloc(m-n); 
strncpy( word3_substr, word3+n, m-1 );

printf( "%s", word3_substr );

return 0; 
}
johnny_bgoode
A: 

I would generalize, building an array of pointers and sorting it with a minimal bubble sort algorithm (or using qsort in the stdlib, better since you've not to extracode), if in ascending order, then first pointer points to the string you need. Even if it's homework, I think you should generalize (what if words are 4, or 5...?) and learn how to generalize such tasks.

 char *ptrs[] = { word1, word2, NULL };
 // later you initilize third too
 ptrs[2] = word3;
 // use qsort with strcmp as comp. function
 qsort(ptrs, sizeof(void *), 3, mwstrcmp);
 // ...
 // pick firts ptr
 char *first = ptrs[0];
 // print last 4 chars, see other answers or:
 // an alternative naive way of getting last 4 chars printed
 int l = strlen(first);
 char *fourstr = first;
 if ( l > 4 ) fourstr += l - 4;
 printf("%s", fourstr); // if length is < 4, it prints the whole string.

EDIT

mwstrcmp is a wrapper that dereference the pointers, since qsort passes pointers to the object (which are pointers...):

int mwstrcmp(const char **a, const char **b)
{
  return strcmp(*a, *b);
}

(warnings are possible too lazy to check now...)

ShinTakezou
A: 

If you want to sort strings, then first stores them into an array and make a Bubble Sort. You can also use this algorithm to sort strings in your linked list.

chanchal1987