Sunday, 21 December 2014

 Function to check substring in a string

 int findstring(char *str,char *temp)
{
int i,j,len1,len2,valid=2;
len1=strlen(str);
len2=strlen(temp);
for(i=0;i<len1;i++)
{
valid=2;
if(str[i-1]==' ')
if(str[i]==temp[0])
for(j=0;j<len2;j++)
{
if(str[j+i]==temp[j])
{valid=1;}
else
{
valid=0;
break;
}
}
if(valid==1 && (str[i+len2]==' ' || str[i+len2]=='.'                    || str[i+len2]==',' || str[i+len2]=='\0'))
{
  
  return i;
   }
}
if(valid!=1)
{ 
  cout<<"\nstring not found ";
  return -1;
        }
}

function to insert a string temp into a              string str at index index


void insertstring(char *str,char *temp,int index)

{
   int j,i,len2,len1;
   len1=strlen(str);
   len2=strlen(temp);
   char temp2[100];
   strcpy(temp2,str+index);
   str[index]='\0';
   strcat(str+index,temp);
   str[index+len2]='\0';
   strcat(str," ");
   strcat(str,temp2);
}

Function to replace a substring(temp) of a string(str) with another substring(rep)


void replace(char *str,char *temp,char *rep)
{
 int index =findstring(str,temp);
int len2=strlen(temp);
char temp2[200];
strcpy(temp2,str+index+len2);
str[index]='\0';
strcat(str,rep);
strcat(str,temp2);
}

    Fuction to append a string 

VOID APPENDSTRING(char *str,char *strappend)
{

  strcat(str,strappend);
}



Q/A

1) Linear insertion sort has just correctly position x[60] in the following array x;
x= 20 40 60 30 10 50       show x after each of x[30] and x[10] is correctly positioned
2) In what situation will linear insertion sort make the fewest interchanges?
answer :
              1) when x[30] correcctly positioned x= 20 30 40 60 10 50
                      when x[10] correcctly positioned x= 10 20 30 40 60 50
                  2) when it is already sorted
Write a recursive java function that copies a binary tree. (Use java only , and please describe your code)
answer:  
              public BinarySearchTree<E> rcopy(){
                          BinarySearchTree<E> newTree = new BinarySearchTree<E>();
                         newTree.root = rcopy(root);
                         newTree.size=newTree.nodes();
                         return newTree;
                         }
                        private Entry <E> rcopy(Entry <E> current){
                       Entry <E> b=new Entry<E>();
                       if(current!=null){
                       if(current.left!=null)b.left=rcopy(current.left);
                       if(current.right!=null)b.right=rcopy(current.right);
                       b.element = current.element;
                       b.parent = successor(current);
                       }
                       return b;
                       }
1. A Boolean expression is one that is either:
   a. true or false 
   b.x or y
   c.Positive or negative
   d.None of these
answer: a