mrblippy
mrblippy

Reputation: 311

C string program

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

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

Upvotes: 15

Views: 561

Answers (5)

chanchal1987
chanchal1987

Reputation: 2357

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.

Upvotes: 0

johnny_bgoode
johnny_bgoode

Reputation: 357

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

Upvotes: 0

ShinTakezou
ShinTakezou

Reputation: 9671

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

Upvotes: 0

brickner
brickner

Reputation: 6585

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

Upvotes: 2

VeeArr
VeeArr

Reputation: 6178

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.

Upvotes: 3

Related Questions