Task: Longest common prefix

C: #include<stdarg.h>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>

char* lcp(int num,...){
	va_list vaList,vaList2;
	int i,j,len,min;
	char* dest;
	char** strings = (char**)malloc(num*sizeof(char*));
	
	va_start(vaList,num);
	va_start(vaList2,num);
	
	for(i=0;i<num;i++){
		len = strlen(va_arg(vaList,char*));
		strings[i] = (char*)malloc((len + 1)*sizeof(char));
		
		strcpy(strings[i],va_arg(vaList2,char*));
		
		if(i==0)
			min = len;
		else if(len<min)
			min = len;
	}
	
	if(min==0)
		return "";
	
	for(i=0;i<min;i++){
		for(j=1;j<num;j++){
			if(strings[j][i]!=strings[0][i]){
				if(i==0)
					return "";
				else{
					dest = (char*)malloc(i*sizeof(char));
					strncpy(dest,strings[0],i-1);
					return dest;
				}
			}
		}
	}
	
	dest = (char*)malloc((min+1)*sizeof(char));
	strncpy(dest,strings[0],min);
	return dest;
}

int main(){

	printf("\nLongest common prefix : %s",lcp(3,"interspecies","interstellar","interstate"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","throne"));
        printf("\nLongest common prefix : %s",lcp(2,"throne","dungeon"));
        printf("\nLongest common prefix : %s",lcp(3,"throne","","throne"));
        printf("\nLongest common prefix : %s",lcp(1,"cheese"));
        printf("\nLongest common prefix : %s",lcp(1,""));
        printf("\nLongest common prefix : %s",lcp(0,NULL));
        printf("\nLongest common prefix : %s",lcp(2,"prefix","suffix"));
        printf("\nLongest common prefix : %s",lcp(2,"foo","foobar"));
	return 0;
}











Ada: with Ada.Text_IO;
with Ada.Strings.Unbounded;

procedure Longest_Prefix is

   package Common_Prefix is
      procedure Reset;
      procedure Consider (Item : in String);
      function Prefix return String;
   end Common_Prefix;

   package body Common_Prefix
   is
      use Ada.Strings.Unbounded;
      Common : Unbounded_String;
      Active : Boolean := False;

      procedure Reset is
      begin
         Common := Null_Unbounded_String;
         Active := False;
      end Reset;

      procedure Consider (Item : in String) is
         Len : constant Natural := Natural'Min (Length (Common), Item'Length);
      begin
         if not Active then
            Active := True;
            Common := To_Unbounded_String (Item);
         else
            for I in 1 .. Len loop
               if Element (Common, I) /= Item (Item'First + I - 1) then
                  Head (Common, I - 1);
                  return;
               end if;
            end loop;
            Head (Common, Len);
         end if;
      end Consider;

      function Prefix return String is (To_String (Common));

   end Common_Prefix;

   use Common_Prefix;
begin
   Consider ("interspecies");   Consider ("interstellar");   Consider ("interstate");
   Ada.Text_IO.Put_Line (Prefix);

   Reset;   Consider ("throne");   Consider ("throne");
   Ada.Text_IO.Put_Line (Prefix);

   Reset;   Consider ("throne");   Consider ("dungeon");
   Ada.Text_IO.Put_Line (Prefix);

   Reset;   Consider ("prefix");   Consider ("suffix");
   Ada.Text_IO.Put_Line (Prefix);

   Reset;   Consider ("foo");   Consider ("foobar");
   Ada.Text_IO.Put_Line (Prefix);

   Reset;   Consider ("foo");
   Ada.Text_IO.Put_Line (Prefix);

   Reset;   Consider ("");
   Ada.Text_IO.Put_Line (Prefix);

   Reset;
   Ada.Text_IO.Put_Line (Prefix);

end Longest_Prefix;