0% found this document useful (0 votes)
18 views13 pages

Lab Exercise: String in Concatenated String

This document describes four lab exercises involving string manipulation and solving puzzles in Java and C/C++. The first exercise involves concatenating two strings and searching for a string in the concatenated string. The second solves the Tower of Hanoi puzzle with two disks. The third solves Tower of Hanoi with three disks. The fourth solves the 8-puzzle problem by moving tiles into the empty space to reach the goal state.

Uploaded by

sani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views13 pages

Lab Exercise: String in Concatenated String

This document describes four lab exercises involving string manipulation and solving puzzles in Java and C/C++. The first exercise involves concatenating two strings and searching for a string in the concatenated string. The second solves the Tower of Hanoi puzzle with two disks. The third solves Tower of Hanoi with three disks. The fourth solves the 8-puzzle problem by moving tiles into the empty space to reach the goal state.

Uploaded by

sani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LAB EXERCISE

Expt. Title 1: Concatenate two strings using C /C++ program and search
string in concatenated string

Description: The problem is concatenate of two string and search string


in concatenated string.
Approach:
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<string.h>
void create();
void print();
void concatenate();
int search();
struct node
{
char data[10];
struct node *link;
};
struct node *head=NULL;
struct node *str1=NULL;
struct node *str2=NULL;
struct node *con=NULL;
int count=0;
1

main()
{
int flag;
printf("\nEnter the first String(Character Wise) :-\n");
create();
print();
printf("\nEnter the Second String(Character Wise) :-\n");
create();
print();
concatenate();
flag=search();
if(flag==1)
printf("\n element is found");
else
printf("element is not found");
getch();
}

void create( ) {
char ch;
do
{
struct node *temp,*current;
temp=(struct node *)malloc(sizeof(struct node));
2

printf("\nEnter the data : ");


scanf(" %s",&temp->data);
if(head==NULL)
{
count++;
head=temp;
current=temp;
}
Else {
current->link=temp;
current=temp;
}
current->link=NULL;
printf("\ndo you want to continue(y/n): ");
ch=getche();
}while(ch!='n');
if(count==1)
{
str1=head;
head=NULL;
}
else
{
str2=head;
3

head=NULL;
}}
void print()
{
struct node *temp;
if(count==1)
{
printf("\nlinked list 1 is :\n");
temp=str1;
while(temp!=NULL)
{
printf("%s",temp->data);
temp=temp->link;
}}
else
{
printf("\nlinked list 2 is :\n");
temp=str2;
while(temp!=NULL)
{
printf("%s",temp->data);
temp=temp->link;
}}}

void concatenate()
{
struct node *temp;
int c=0;
printf("\n\nConcatenated String is :\n");
temp=str1;
while(temp!=NULL)
{
printf("%s",temp->data);
temp=temp->link;
printf(" ");
if(temp==NULL && c==0)
{
temp=str2;
c++;
}}}
int search()
{
int flag=0,c=0;
char str[10];
struct node *temp;
printf("\n\nwhat searched element: ");
scanf("%s",&str);

temp=str1;
while(temp!=NULL)
{
if(strcmp(temp->data,str)==0)
return(1);
temp=temp->link;
if(temp==NULL && c==0) {
temp=str2;
c++;
}}
if(flag==0)
return(0); }

Result:

Expt. Title 2: Solve Tower of Hanoi with only 2 disks.


6

Description: The Tower of Hanoi is a mathematical game or puzzle. It


consists of three rods, and a number of disks of different
sizes which can slide onto any rod. The puzzle starts with the
disks in a neat stack in ascending order of size on one rod,
the smallest at the top, thus making a conical shape.
The objective of the puzzle is to move the entire stack to another rod,
obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks
and placing it on top of another stack i.e. a disk can only be moved if
it is the uppermost disk on a stack.
3. No disk may be placed on top of a smaller disk.

Approach:
#include <stdio.h>
#include<conio.h>
void move(int,char,char,char);
main( )
{
int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("\nMoves of the Tower of Hanoi :\n");
move(num, 'A', 'C', 'B');
getch();

void move(int num, char from, char to, char inter)


7

{
if (num==1)
{
printf("\n disk 1 : from stack %c to stack %c", from, to);
return;
}
move(num - 1, from, inter, to);
printf("\n disk %d : from stack %c to stack %c", num, from, to);
move(num - 1, inter, to, from);
}

Result:

Expt. Title 3: Solve Tower of Hanoi with only 3 disks.


Approach:
#include <stdio.h>
#include<conio.h>
void move(int,char,char,char);
main( )
{
8

int num;
printf("Enter the number of disks : ");
scanf("%d", &num);
printf("\nMoves of the Tower of Hanoi :\n");
move(num, 'A', 'C', 'B');
getch( ); }
void move(int num, char from, char to, char inter) {
if (num==1) {
printf("\n disk 1 : from stack %c to stack %c", from, to);
return;
}
move(num - 1, from, inter, to);
printf("\n disk %d : from stack %c to stack %c", num, from, to);
move(num - 1, inter, to, from);
}

Result:

Expt. Title 4: Solve 8 puzzle take any initial and goal state.
Description:

The 8-puzzle problem is a puzzle. It is played on a 3-by-3 grid with 8


square blocks labeled 1 through 8 and a blank square. Your goal is to
rearrange the blocks so that they are in order. You are permitted to slide
blocks horizontally or vertically into the blank square.

Approach:
import java.util.*;
import java.io.*;
class EightPuzzle
{
Queue<String> q = new LinkedList<String>();
Map<String,Integer> map = new HashMap<String, Integer>();
public static void main(String args[]){
try {
BufferedReader br = new BufferedReader(newInputStreamReader(System.in));
String str=br.readLine();
EightPuzzle e = new EightPuzzle();
e.add(str,0);
while(e.q.peek()!=null) {
e.up(e.q.peek());
e.down(e.q.peek());
e.left(e.q.peek());
e.right(e.q.remove()); }
System.out.println("Solution doesn't exist");
}
10

catch (Exception e){ }


}
void add(String str,int n)
{
if(!map.containsKey(str)) {
map.put(str,n);
q.add(str);
}}

void up(String str)


{
int a = str.indexOf("0");
if(a>2) {
String s = str.substring(0,a-3)+"0"+str.substring(a-2,a)+str.charAt
(a-3)+str.substring(a+1);
add(s,map.get(str)+1);
if(s.equals("123456780")) {
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}}}

void down(String str) {


int a = str.indexOf("0");
if(a<6) {
11

String s = str.substring(0,a)+str.substring(a+3,a+4)+str.substring(a+1,a+3)+"0"
+str.substring(a+4);
add(s,map.get(str)+1);
if(s.equals("123456780"))
{
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}}}
void left(String str)
{
int a = str.indexOf("0");
if(a!=0 && a!=3 && a!=6){
String s = str.substring(0,a-1)+"0"+str.charAt(a-1)+str.substring(a+1);
add(s,map.get(str)+1);
if(s.equals("123456780"))
{
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}}}

void right(String str) {


int a = str.indexOf("0");
if(a!=2 && a!=5 && a!=8) {

12

String s = str.substring(0,a)+str.charAt(a+1)+"0"+str.substring(a+2);
add(s,map.get(str)+1);
if(s.equals("123456780")) {
System.out.println("Solution Exists at Level "+map.get(s)+" of the tree");
System.exit(0);
}}}}

Result:
Solution Exists at Level 3 of the tree

13

You might also like