15

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;
}
9
  • 8
    +1 for a homework question with actual code! Commented Jun 7, 2010 at 4:30
  • 1
    scanf( "%s", word ) is unsafe. You are asking for a buffer overflow error. Simply enter a string that exceeds 100 characters and BOOM--buffer overflow. Limit the max size of the scan with scanf( "%100s", word ). See en.wikipedia.org/wiki/Scanf#Security for more info. Commented Jun 7, 2010 at 6:14
  • Also be careful with strcpy--this function is the cause of many buffer overflow vulnerabilities. Consider using strncpy, which limits the maximum size of the string, instead. Even still, with strncpy the final character copied is not guaranteed to be \x00, so be sure to set the final character after you copy the data. Losing string terminators can cause major headaches. Commented Jun 7, 2010 at 6:21
  • 1
    Note that you can safely ignore suggestions to use complicated sorting algorithms. A naive sorting implemenation will be good enough for your purposes - you're only sorting 4 characters! Commented Jun 7, 2010 at 8:36
  • 2
    @graham.needs - whilst I agree with your point, any opportunity to educate someone who's learning to program in ways to do so more securely can never be a bad thing =) Commented Jun 7, 2010 at 8:44

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, how would i use strcmp on the three strings though? sorry i am new to this
2

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;
}

3 Comments

Thank you for your reply,i am aware that i have to do this. But i need help implementing it
The link he provided shows examples of how to use strcmp().
Thank you so much, thats the code i had in mind, just wasnt quite sure how to write it
0

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...)

Comments

0

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; 
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.