Showing posts with label Palindrome. Show all posts
Showing posts with label Palindrome. Show all posts

TCP Client-Server Program - Check Palindrome - Network-DBMS Lab - C program

Program:

// Client Program: pltcpc.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
main()
{
   struct sockaddr_in client;
   int s,flag;
   char buffer[20];
   s=socket(AF_INET,SOCK_STREAM,0);
   client.sin_family=AF_INET;
   client.sin_port=2000;
   client.sin_addr.s_addr=inet_addr("127.0.0.1");
   connect(s,(struct sockaddr *)&client,sizeof client);
   for(;;)
    {
    printf("\nEnter a string to check palindrome: ");
    scanf("%s",buffer);
    printf("\nClient: %s",buffer);
    send(s,buffer,sizeof(buffer),0);
    recv(s,&flag,sizeof(int),0);
    if(flag==1)
    {
      printf("\nServer: The string is a Palindrome.\n");
        break;
    }
    else
    {
       printf("\nServer: The string is not a palindrome.\n");
        break;
    }
     }
    close(s);
}

// Server program: pltcps.c

#include<stdio.h>
#include<string.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>

main()
{
  struct sockaddr_in client,server;
  int s,n,sock,g,j,left,right,flag;
  char b1[20],b2[10],b3[10],b4[10];
  s=socket(AF_INET,SOCK_STREAM,0);
  server.sin_family=AF_INET;
  server.sin_port=2000;
  server.sin_addr.s_addr=inet_addr("127.0.0.1");
  bind(s,(struct sockaddr *)&server,sizeof server);
  listen(s,1);
  n=sizeof client;
  sock=accept(s,(struct sockaddr *)&client,&n);
  for(;;)
   {
      recv(sock,b1,sizeof(b1),0);
      printf("\nThe string received is:%s\n",b1);
      if(strlen(b1)==0)
    flag=1;
      else
      {
    left=0;
        right=strlen(b1)-1;
        flag=1;
        while(left<right && flag)
        {
        if (b1[left]!= b1[right])
        flag=0;
        else
        {
            left++;
            right--;
        }
        }
      }
      send(sock,&flag,sizeof(int),0);
      break;
   } 
  close(sock);
  close(s);
}

Output :

Terminal 1: (Client)
nn@linuxmint ~ $ gcc pltcpc.c -o client
nn@linuxmint ~ $ ./client

Enter a string to check palindrome: malayalam

Client: malayalam
Server: The string is a Palindrome.
nn@linuxmint ~ $

Terminal 2: (Server)
nn@linuxmint ~ $ gcc pltcps.c -o server
nn@linuxmint ~ $ ./server

The string received is:malayalam
nn@linuxmint ~ $


Palindrome Check - Client-Server Program - Network-DBMS Lab - Java program


Program:

//ServerCode2 .java

import java.io.*;
import java.net.*;

public class ServerCode2 {
ServerSocket ss;
Socket socket;
BufferedReader sock_in,kdb_in;
PrintWriter sock_out;
String str;
    public ServerCode2()
    {
    try{
        ss=new ServerSocket(8765);
        System.out.println("Server is listening port 8765");
        socket=ss.accept();
        System.out.println("Connection established...");
        kdb_in=new BufferedReader(new InputStreamReader(System.in));
        sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sock_out=new PrintWriter(socket.getOutputStream());
        while(true)
        {
        System.out.println("Msg from client");
        str=sock_in.readLine();
        int k=str.length();
        System.out.println(str);
        int left=0,right=k-1;int flag=1;
        while(left<=right)
        {
            if(str.charAt(left)!=(str.charAt(right)))
            {
                flag=0;
                break;
            }
            else
            {
                left++;right--;
            }
        }
        if(flag==1)
            str="....Palindrome....";
        else
            str="....Not Palindrome....";
        //System.out.println("Enter the msg");
        //str=kdb_in.readLine();
        sock_out.println(str);
        sock_out.flush();
        if(str.equals("bye"))
            break;
        }
    }catch (Exception e) { }
    }
public static void main(String arg[])
{
    new ServerCode2();
}}


//ClientCode.java

import java.io.*;
import java.net.*;

public class ClientCode{

Socket socket;
BufferedReader sock_in,kdb_in;
PrintWriter sock_out;
String str;
    public ClientCode()
    {
    try{

        Socket socket=new Socket("127.0.0.1",8765);
        kdb_in=new BufferedReader(new InputStreamReader(System.in));
        sock_in=new BufferedReader(new InputStreamReader(socket.getInputStream()));
        sock_out=new PrintWriter(socket.getOutputStream());
        while(true)
        {
       
        System.out.println("Enter the msg");
        str=kdb_in.readLine();
        sock_out.println(str);
        sock_out.flush();
        System.out.println("Msg from Server");
        str=sock_in.readLine();
        System.out.println(str);
        if(str.equals("bye"))
            break;
        }
        socket.close();
    }catch (Exception e) { }
    }
public static void main(String arg[])
{
    new ClientCode();
}}

Output:

Terminal 1 (Server):

students@ccflab-desktop:~$ javac ServerCode2.java
students@ccflab-desktop:~$ java ServerCode2
Server is listening port 8765
Connection established...
Msg from client
malayalam
Msg from client
abc
Msg from client

Terminal 2(Client):

students@ccflab-desktop:~$ javac ClientCode.java
students@ccflab-desktop:~$ java ClientCode
Enter the msg
malayalam
Msg from Server
....Palindrome....
Enter the msg
abc
Msg from Server
....Not Palindrome....
Enter the msg

Check Palindrome - LISP

Program:
(defun palindrome()
(format t "Enter the string(in \"\"):")
(setf x (read))
(if(string= x (reverse x))
    (format t "palindrome")
    (format t "Not palindrome")
    )                                                                          
)


Output:
[1]> (load 'pal.lsp)
;; Loading file pal.lsp ...
;; Loaded file pal.lsp
T
[2]> (palindrome)
Enter the string(in ""):"malayalam"
palindrome
NIL
[3]> (palindrome)
Enter the string(in ""):"HAI"
Not palindrome
NIL
 [4]>


Concatenate String & Check Palindrome - Assembly Language - MASM

Program:
.model small
.data
    mes1 db 10,13,"enter the first string",10,13,"$"
    mes2 db 10,13,"enter the second string",10,13,"$"
    mes3 db 10,13,"the concatinated string is:",10,13,"$"
    mes4 db 10,13,"the string is a palindrome",10,13,"$"
    mes5 db 10,13,"the string is not a palindrome",10,13,"$"
    arr db 100  

.code
.startup

    lea dx,mes1
    mov ah,09h
    int 21h
    lea dx,arr
    mov ah,0ah
    int 21h

    mov si,2
    mov bl,arr[1]
    mov bh,00h
    mov di,bx
    inc di
    inc di

    lea dx,mes2
    mov ah,09h
    int 21h

l1:
   mov ah,01h
   int 21h
   cmp al,13
   je l2
   mov arr[di],al
   inc di
   jmp l1

l2:
   lea dx,mes3
   mov ah,09h
   int 21h

l3:
   cmp si,di
   je l4
   mov dl,arr[si]
   mov ah,02h
   int 21h
   inc si
   jmp l3

l4:
    mov si,2
    dec di
    
l5:
     mov bl,arr[si]
     mov bh,00h
     mov cl,arr[di]
     mov ch,00h

     cmp bl,cl
     jne l6
     cmp di,si
     je l7
     inc si
     dec di
     jmp l5

l6:
     lea dx,mes5
     mov ah,09h
     int 21h
     jmp exit1

l7:
     lea dx,mes4
     mov ah,09h
     int 21h
     jmp exit1

exit1:
   .exit
   end

  

Palindrome - Assembly Language - MASM


PALINDROME OR NOT

Program:
.model small
.stack 200H
.data
   msg1 DB 10,13,'enter the string: $'
   msg2 DB 10,13,'PALINDROME $'
   msg3 DB 10,13,'NOT PALINDROME $'
   newline DB 10,13,' $'
   count db ?
   tab db 20 dup(?)
.code
   print MACRO msg ;macro definition
   PUSH AX
   PUSH DX
   MOV AH,09H
   MOV DX,offset msg
   INT 21H
   POP DX
   POP AX
   ENDM
.startup
   print msg1
   print newline
   mov si ,offset tab
   mov di,offset tab
   mov dx,00
   mov cx,00
rdnxt:
   mov ah,01h
   int 21h
   mov [si],al
   inc si
   inc dx
   cmp al,'$'
   jne rdnxt
   dec si
   dec si
   dec dx
   mov cx,dx
   print newline
nxtchar:
   mov dl,[si]
   mov dh,[di]
   cmp dl,dh
   jne palin
   inc di
   dec si
   loop nxtchar
   print msg2
   jmp ext
palin:
   print msg3
ext:
   .exit
END

Output:
F:\>pal
enter the string:
malayalam$
PALINDROME
F:\>pal
enter the string:
computer$
NOT PALINDROME

Check Palindrome or not - C Program

Program:
#include<stdio.h>
#include<string.h>
//#include<conio.h>
int ispalindrome(char str[20])
{
     int left,right,flag;
     if(strlen(str)==0)
        return 1;
     else
     {
        left=0;
        right=strlen(str)-1;
        flag=1;
        while(left<right && flag)
        {
         if (str[left]!= str[right])
            flag=0;
         else
            {
             left++;
             right--;
            }
        }
       return flag;
     }
}
void main()
{
     char str[200];
     printf("Enter the string: ");
     scanf("%s",str);
     if(ispalindrome(str))
        printf("String is palindrome.\n");
     else
     printf("String is not palindrome.\n");
//     getch();
}

Output:
nn@linuxmint ~ $ gcc c10.c
nn@linuxmint ~ $ ./a.out
Enter the string: malayalam
String is palindrome.
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...