Lab Exercise: String in Concatenated String
Lab Exercise: String in Concatenated String
Expt. Title 1: Concatenate two strings using C /C++ program and search
string in concatenated string
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
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:
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();
{
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:
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:
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
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);
}}}
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