//String Length #include <stdio.h> void main() { char string[50]; int i, length = 0; printf("Enter a string n"); gets(string); for (i = 0; string[i] != '0'; i++) { length++; } printf("The length of %s = %dn", string, length); }
// String Concatenation #include<stdio.h> void main(void) { char str1[25],str2[25]; int i=0, j=0; printf("nEnter First String:"); gets(str1); printf("nEnter Second String:"); gets(str2); while(str1[i]!='0') i++; while(str2[j]!='0') { str1[i]=str2[j]; j++; i++; } str1[i]='0'; printf("nConcatenated String is %s",str1); }
//Pattern Matching # include<stdio.h> void main() { char str[80], search[10]; int count1 = 0, count2 = 0, i, j, flag; printf("Enter a string:"); gets(str); printf("Enter search substring:"); gets(search); while (str[count1] != '0') count1++; while (search[count2] != '0') count2++; for (i = 0; i <= count1 - count2; i++) { for (j = i; j < i + count2; j++) { flag = 1; if (str[j] != search[j - i]) { flag = 0; break; }
} if (flag == 1) break; } if (flag == 1) printf("SEARCH SUCCESSFUL!"); else printf("SEARCH UNSUCCESSFUL!"); }
// Program to Implement B+ Tree #include<stdio.h> #include<conio.h> #include<iostream> using namespace std; struct BTreeNode { int *data; BTreeNode **child_ptr; bool leaf; int n; }*root = NULL, *np = NULL, *x = NULL; BTreeNode * init() { int i; np = new BTreeNode; np->data = new int[5]; np->child_ptr = new BTreeNode *[6]; np->leaf = true; np->n = 0; for (i = 0; i < 6; i++) { np->child_ptr[i] = NULL; } return np;
} void traverse(BTreeNode *p) { cout<<endl; int i; for (i = 0; i < p->n; i++) { if (p->leaf == false) { traverse(p->child_ptr[i]); } cout << " " << p->data[i]; } if (p->leaf == false) { traverse(p->child_ptr[i]); } cout<<endl; } void sort(int *p, int n) { int i, j, temp; for (i = 0; i < n; i++) { for (j = i; j <= n; j++) {
if (p[i] > p[j]) { temp = p[i]; p[i] = p[j]; p[j] = temp; } } } } int split_child(BTreeNode *x, int i) { int j, mid; BTreeNode *np1, *np3, *y; np3 = init(); np3->leaf = true; if (i == -1) { mid = x->data[2]; x->data[2] = 0; x->n--; np1 = init(); np1->leaf = false; x->leaf = true; for (j = 3; j < 5; j++) { np3->data[j - 3] = x->data[j];
np3->child_ptr[j - 3] = x->child_ptr[j]; np3->n++; x->data[j] = 0; x->n--; } for(j = 0; j < 6; j++) { x->child_ptr[j] = NULL; } np1->data[0] = mid; np1->child_ptr[np1->n] = x; np1->child_ptr[np1->n + 1] = np3; np1->n++; root = np1; } else { y = x->child_ptr[i]; mid = y->data[2]; y->data[2] = 0; y->n--; for (j = 3; j < 5; j++) { np3->data[j - 3] = y->data[j]; np3->n++; y->data[j] = 0;
y->n--; } x->child_ptr[i + 1] = y; x->child_ptr[i + 1] = np3; } return mid; } void insert(int a) { int i, temp; x = root; if (x == NULL) { root = init(); x = root; } else { if (x->leaf == true && x->n == 5) { temp = split_child(x, -1); x = root; for (i = 0; i < (x->n); i++) { if ((a > x->data[i]) && (a < x->data[i + 1])) {
i++; break; } else if (a < x->data[0]) { break; } else { continue; } } x = x->child_ptr[i]; } else { while (x->leaf == false) { for (i = 0; i < (x->n); i++) { if ((a > x->data[i]) && (a < x->data[i + 1])) { i++; break; } else if (a < x->data[0])
{ break; } else { continue; } } if ((x->child_ptr[i])->n == 5) { temp = split_child(x, i); x->data[x->n] = temp; x->n++; continue; } else { x = x->child_ptr[i]; } } } } x->data[x->n] = a; sort(x->data, x->n); x->n++; }
int main() { int i, n, t; cout<<"enter the no of elements to be insertedn"; cin>>n; for(i = 0; i < n; i++) { cout<<"enter the elementn"; cin>>t; insert(t); } cout<<"traversal of constructed treen"; traverse(root); getch(); }
//Threaded Binary Tree #include <iostream> #include <cstdlib> #define MAX_VALUE 65536 using namespace std; /* Class Node */ class Node { public: int key; Node *left, *right; bool leftThread, rightThread; }; /* Class ThreadedBinarySearchTree */ class ThreadedBinarySearchTree { private: Node *root; public: /* Constructor */ ThreadedBinarySearchTree() {
root = new Node(); root->right = root->left = root; root->leftThread = true; root->key = MAX_VALUE; } /* Function to clear tree */ void makeEmpty() { root = new Node(); root->right = root->left = root; root->leftThread = true; root->key = MAX_VALUE; } /* Function to insert a key */ void insert(int key) { Node *p = root; for (;;) { if (p->key < key) { if (p->rightThread) break; p = p->right; }
else if (p->key > key) { if (p->leftThread) break; p = p->left; } else { /* redundant key */ return; } } Node *tmp = new Node(); tmp->key = key; tmp->rightThread = tmp->leftThread = true; if (p->key < key) { /* insert to right side */ tmp->right = p->right; tmp->left = p; p->right = tmp; p->rightThread = false; } else {
tmp->right = p; tmp->left = p->left; p->left = tmp; p->leftThread = false; } } /* Function to search for an element */ bool search(int key) { Node *tmp = root->left; for (;;) { if (tmp->key < key) { if (tmp->rightThread) return false; tmp = tmp->right; } else if (tmp->key > key) { if (tmp->leftThread) return false; tmp = tmp->left; } else
{ return true; } } } /* Fuction to delete an element */ void Delete(int key) { Node *dest = root->left, *p = root; for (;;) { if (dest->key < key) { /* not found */ if (dest->rightThread) return; p = dest; dest = dest->right; } else if (dest->key > key) { /* not found */ if (dest->leftThread) return; p = dest;
dest = dest->left; } else { /* found */ break; } } Node *target = dest; if (!dest->rightThread && !dest->leftThread) { /* dest has two children*/ p = dest; /* find largest node at left child */ target = dest->left; while (!target->rightThread) { p = target; target = target->right; } /* using replace mode*/ dest->key = target->key; } if (p->key >= target->key) {
if (target->rightThread && target->leftThread) { p->left = target->left; p->leftThread = true; } else if (target->rightThread) { Node *largest = target->left; while (!largest->rightThread) { largest = largest->right; } largest->right = p; p->left = target->left; } else { Node *smallest = target->right; while (!smallest->leftThread) { smallest = smallest->left; } smallest->left = target->left; p->left = target->right; }
} else { if (target->rightThread && target->leftThread) { p->right = target->right; p->rightThread = true; } else if (target->rightThread) { Node *largest = target->left; while (!largest->rightThread) { largest = largest->right; } largest->right = target->right; p->right = target->left; } else { Node *smallest = target->right; while (!smallest->leftThread) { smallest = smallest->left; }
smallest->left = p; p->right = target->right; } } } /* Function to print tree */ void printTree() { Node *tmp = root, *p; for (;;) { p = tmp; tmp = tmp->right; if (!p->rightThread) { while (!tmp->leftThread) { tmp = tmp->left; } } if (tmp == root) break; cout<<tmp->key<<" "; } cout<<endl;
} }; int main() { ThreadedBinarySearchTree tbst; cout<<"ThreadedBinarySearchTree Testn"; char ch; int choice, val; /* Perform tree operations */ do { cout<<"nThreadedBinarySearchTree Operationsn"; cout<<"1. Insert "<<endl; cout<<"2. Delete"<<endl; cout<<"3. Search"<<endl; cout<<"4. Clear"<<endl; cout<<"Enter Your Choice: "; cin>>choice; switch (choice) { case 1 : cout<<"Enter integer element to insert: "; cin>>val; tbst.insert(val);
break; case 2 : cout<<"Enter integer element to delete: "; cin>>val; tbst.Delete(val); break; case 3 : cout<<"Enter integer element to search: "; cin>>val; if (tbst.search(val) == true) cout<<"Element "<<val<<" found in the tree"<<endl; else cout<<"Element "<<val<<" not found in the tree"<<endl; break; case 4 : cout<<"nTree Clearedn"; tbst.makeEmpty(); break; default : cout<<"Wrong Entry n "; break; } /* Display tree */ cout<<"nTree = "; tbst.printTree();
cout<<"nDo you want to continue (Type y or n): "; cin>>ch; } while (ch == 'Y'|| ch == 'y'); return 0; }
//To Copy one string into other #include <stdio.h> int main() { char s1[100], s2[100], i; printf("Enter string s1: "); gets(s1); printf("Enter string s2: "); gets(s2); for(i = 0; s1[i] != '0'; ++i) { s2[i] = s1[i]; } s2[i] = '0'; printf("String s1: %s", s1); printf("nString s2: %s", s2); return 0; }
// Read from binary file #include <stdio.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","rb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) {
fread(&num, sizeof(struct threeNum), 1, fptr); printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3); } fclose(fptr); return 0; }
//To count number of words #include<stdio.h> void main() { FILE *p; char ch; int w=1; clrscr(); p=fopen("source","r"); if(p==NULL) { printf("file not found"); } else { ch=fgetc(p); while(ch!=EOF) { printf("%c",ch); if(ch==' '||ch=='n') { w++; } ch=fgetc(p); } printf("nWords in a file are=%d",w); } fclose(p); getch(); }
//writing into a binary file #include <stdio.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","wb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) { num.n1 = n; num.n2 = 5n; num.n3 = 5n + 1; fwrite(&num, sizeof(struct threeNum), 1, fptr); } fclose(fptr); return 0;

program on string in java Lab file 2 (3-year)

  • 1.
    //String Length #include <stdio.h> voidmain() { char string[50]; int i, length = 0; printf("Enter a string n"); gets(string); for (i = 0; string[i] != '0'; i++) { length++; } printf("The length of %s = %dn", string, length); }
  • 2.
    // String Concatenation #include<stdio.h> voidmain(void) { char str1[25],str2[25]; int i=0, j=0; printf("nEnter First String:"); gets(str1); printf("nEnter Second String:"); gets(str2); while(str1[i]!='0') i++; while(str2[j]!='0') { str1[i]=str2[j]; j++; i++; } str1[i]='0'; printf("nConcatenated String is %s",str1); }
  • 3.
    //Pattern Matching # include<stdio.h> voidmain() { char str[80], search[10]; int count1 = 0, count2 = 0, i, j, flag; printf("Enter a string:"); gets(str); printf("Enter search substring:"); gets(search); while (str[count1] != '0') count1++; while (search[count2] != '0') count2++; for (i = 0; i <= count1 - count2; i++) { for (j = i; j < i + count2; j++) { flag = 1; if (str[j] != search[j - i]) { flag = 0; break; }
  • 4.
    } if (flag ==1) break; } if (flag == 1) printf("SEARCH SUCCESSFUL!"); else printf("SEARCH UNSUCCESSFUL!"); }
  • 5.
    // Program toImplement B+ Tree #include<stdio.h> #include<conio.h> #include<iostream> using namespace std; struct BTreeNode { int *data; BTreeNode **child_ptr; bool leaf; int n; }*root = NULL, *np = NULL, *x = NULL; BTreeNode * init() { int i; np = new BTreeNode; np->data = new int[5]; np->child_ptr = new BTreeNode *[6]; np->leaf = true; np->n = 0; for (i = 0; i < 6; i++) { np->child_ptr[i] = NULL; } return np;
  • 6.
    } void traverse(BTreeNode *p) { cout<<endl; inti; for (i = 0; i < p->n; i++) { if (p->leaf == false) { traverse(p->child_ptr[i]); } cout << " " << p->data[i]; } if (p->leaf == false) { traverse(p->child_ptr[i]); } cout<<endl; } void sort(int *p, int n) { int i, j, temp; for (i = 0; i < n; i++) { for (j = i; j <= n; j++) {
  • 7.
    if (p[i] >p[j]) { temp = p[i]; p[i] = p[j]; p[j] = temp; } } } } int split_child(BTreeNode *x, int i) { int j, mid; BTreeNode *np1, *np3, *y; np3 = init(); np3->leaf = true; if (i == -1) { mid = x->data[2]; x->data[2] = 0; x->n--; np1 = init(); np1->leaf = false; x->leaf = true; for (j = 3; j < 5; j++) { np3->data[j - 3] = x->data[j];
  • 8.
    np3->child_ptr[j - 3]= x->child_ptr[j]; np3->n++; x->data[j] = 0; x->n--; } for(j = 0; j < 6; j++) { x->child_ptr[j] = NULL; } np1->data[0] = mid; np1->child_ptr[np1->n] = x; np1->child_ptr[np1->n + 1] = np3; np1->n++; root = np1; } else { y = x->child_ptr[i]; mid = y->data[2]; y->data[2] = 0; y->n--; for (j = 3; j < 5; j++) { np3->data[j - 3] = y->data[j]; np3->n++; y->data[j] = 0;
  • 9.
    y->n--; } x->child_ptr[i + 1]= y; x->child_ptr[i + 1] = np3; } return mid; } void insert(int a) { int i, temp; x = root; if (x == NULL) { root = init(); x = root; } else { if (x->leaf == true && x->n == 5) { temp = split_child(x, -1); x = root; for (i = 0; i < (x->n); i++) { if ((a > x->data[i]) && (a < x->data[i + 1])) {
  • 10.
    i++; break; } else if (a< x->data[0]) { break; } else { continue; } } x = x->child_ptr[i]; } else { while (x->leaf == false) { for (i = 0; i < (x->n); i++) { if ((a > x->data[i]) && (a < x->data[i + 1])) { i++; break; } else if (a < x->data[0])
  • 11.
    { break; } else { continue; } } if ((x->child_ptr[i])->n ==5) { temp = split_child(x, i); x->data[x->n] = temp; x->n++; continue; } else { x = x->child_ptr[i]; } } } } x->data[x->n] = a; sort(x->data, x->n); x->n++; }
  • 12.
    int main() { int i,n, t; cout<<"enter the no of elements to be insertedn"; cin>>n; for(i = 0; i < n; i++) { cout<<"enter the elementn"; cin>>t; insert(t); } cout<<"traversal of constructed treen"; traverse(root); getch(); }
  • 13.
    //Threaded Binary Tree #include<iostream> #include <cstdlib> #define MAX_VALUE 65536 using namespace std; /* Class Node */ class Node { public: int key; Node *left, *right; bool leftThread, rightThread; }; /* Class ThreadedBinarySearchTree */ class ThreadedBinarySearchTree { private: Node *root; public: /* Constructor */ ThreadedBinarySearchTree() {
  • 14.
    root = newNode(); root->right = root->left = root; root->leftThread = true; root->key = MAX_VALUE; } /* Function to clear tree */ void makeEmpty() { root = new Node(); root->right = root->left = root; root->leftThread = true; root->key = MAX_VALUE; } /* Function to insert a key */ void insert(int key) { Node *p = root; for (;;) { if (p->key < key) { if (p->rightThread) break; p = p->right; }
  • 15.
    else if (p->key> key) { if (p->leftThread) break; p = p->left; } else { /* redundant key */ return; } } Node *tmp = new Node(); tmp->key = key; tmp->rightThread = tmp->leftThread = true; if (p->key < key) { /* insert to right side */ tmp->right = p->right; tmp->left = p; p->right = tmp; p->rightThread = false; } else {
  • 16.
    tmp->right = p; tmp->left= p->left; p->left = tmp; p->leftThread = false; } } /* Function to search for an element */ bool search(int key) { Node *tmp = root->left; for (;;) { if (tmp->key < key) { if (tmp->rightThread) return false; tmp = tmp->right; } else if (tmp->key > key) { if (tmp->leftThread) return false; tmp = tmp->left; } else
  • 17.
    { return true; } } } /* Fuctionto delete an element */ void Delete(int key) { Node *dest = root->left, *p = root; for (;;) { if (dest->key < key) { /* not found */ if (dest->rightThread) return; p = dest; dest = dest->right; } else if (dest->key > key) { /* not found */ if (dest->leftThread) return; p = dest;
  • 18.
    dest = dest->left; } else { /*found */ break; } } Node *target = dest; if (!dest->rightThread && !dest->leftThread) { /* dest has two children*/ p = dest; /* find largest node at left child */ target = dest->left; while (!target->rightThread) { p = target; target = target->right; } /* using replace mode*/ dest->key = target->key; } if (p->key >= target->key) {
  • 19.
    if (target->rightThread &&target->leftThread) { p->left = target->left; p->leftThread = true; } else if (target->rightThread) { Node *largest = target->left; while (!largest->rightThread) { largest = largest->right; } largest->right = p; p->left = target->left; } else { Node *smallest = target->right; while (!smallest->leftThread) { smallest = smallest->left; } smallest->left = target->left; p->left = target->right; }
  • 20.
    } else { if (target->rightThread &&target->leftThread) { p->right = target->right; p->rightThread = true; } else if (target->rightThread) { Node *largest = target->left; while (!largest->rightThread) { largest = largest->right; } largest->right = target->right; p->right = target->left; } else { Node *smallest = target->right; while (!smallest->leftThread) { smallest = smallest->left; }
  • 21.
    smallest->left = p; p->right= target->right; } } } /* Function to print tree */ void printTree() { Node *tmp = root, *p; for (;;) { p = tmp; tmp = tmp->right; if (!p->rightThread) { while (!tmp->leftThread) { tmp = tmp->left; } } if (tmp == root) break; cout<<tmp->key<<" "; } cout<<endl;
  • 22.
    } }; int main() { ThreadedBinarySearchTree tbst; cout<<"ThreadedBinarySearchTreeTestn"; char ch; int choice, val; /* Perform tree operations */ do { cout<<"nThreadedBinarySearchTree Operationsn"; cout<<"1. Insert "<<endl; cout<<"2. Delete"<<endl; cout<<"3. Search"<<endl; cout<<"4. Clear"<<endl; cout<<"Enter Your Choice: "; cin>>choice; switch (choice) { case 1 : cout<<"Enter integer element to insert: "; cin>>val; tbst.insert(val);
  • 23.
    break; case 2 : cout<<"Enterinteger element to delete: "; cin>>val; tbst.Delete(val); break; case 3 : cout<<"Enter integer element to search: "; cin>>val; if (tbst.search(val) == true) cout<<"Element "<<val<<" found in the tree"<<endl; else cout<<"Element "<<val<<" not found in the tree"<<endl; break; case 4 : cout<<"nTree Clearedn"; tbst.makeEmpty(); break; default : cout<<"Wrong Entry n "; break; } /* Display tree */ cout<<"nTree = "; tbst.printTree();
  • 24.
    cout<<"nDo you wantto continue (Type y or n): "; cin>>ch; } while (ch == 'Y'|| ch == 'y'); return 0; }
  • 25.
    //To Copy onestring into other #include <stdio.h> int main() { char s1[100], s2[100], i; printf("Enter string s1: "); gets(s1); printf("Enter string s2: "); gets(s2); for(i = 0; s1[i] != '0'; ++i) { s2[i] = s1[i]; } s2[i] = '0'; printf("String s1: %s", s1); printf("nString s2: %s", s2); return 0; }
  • 26.
    // Read frombinary file #include <stdio.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","rb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) {
  • 27.
    fread(&num, sizeof(struct threeNum),1, fptr); printf("n1: %dtn2: %dtn3: %d", num.n1, num.n2, num.n3); } fclose(fptr); return 0; }
  • 28.
    //To count numberof words #include<stdio.h> void main() { FILE *p; char ch; int w=1; clrscr(); p=fopen("source","r"); if(p==NULL) { printf("file not found"); } else { ch=fgetc(p); while(ch!=EOF) { printf("%c",ch); if(ch==' '||ch=='n') { w++; } ch=fgetc(p); } printf("nWords in a file are=%d",w); } fclose(p); getch(); }
  • 29.
    //writing into abinary file #include <stdio.h> struct threeNum { int n1, n2, n3; }; int main() { int n; struct threeNum num; FILE *fptr; if ((fptr = fopen("C:program.bin","wb")) == NULL){ printf("Error! opening file"); // Program exits if the file pointer returns NULL. exit(1); } for(n = 1; n < 5; ++n) { num.n1 = n; num.n2 = 5n; num.n3 = 5n + 1; fwrite(&num, sizeof(struct threeNum), 1, fptr); } fclose(fptr); return 0;