Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

PHP Examples - Strings - Substring - String Replacement - Reversal - Trim

PHP Examples - Strings
Substring - String Replacement - Reversal - Trim

Important Functions

1. Stringlength                       -            strlen(string)
2. Stringreversal                    -           strrev(string)
3. Substring                          -            substr(string, starting position, length)
4. Replace Substring              -            str_replace(new_substring, old_substring, string)
5. Trim (Removing Blank Spaces) -     rtrim(string) [ Remove blank space from right]

                                                   -     ltrim(string) [ Remove blank space from left]

Program:
<html >
<head>

<title>PHP Example 4 : STRING</title>
</head>

<body>
<?php
 
 echo "PHP Example 4";
 echo"<br/>"; // line break
 echo"<h1>";
 echo"STRINGS";
 echo"</h1>";
 echo"<br/><li>";
 $string1 = "I LOVE 2K8CSE";
 echo"String: $string1";
 echo"<br/></li>";
 echo"<br/><li>";
 $stringlength= strlen($string1);   //Stringlength
 echo"length: $stringlength";
 echo"<br/></li>";
 echo"<br/><li>";
 $string2 = substr($string1,2,4); //Substring 
 echo" Substring (2,4): $string2";
 echo"<br/></li>";
 echo"<br/><li>";
 $string3= str_replace("I","WE",$string1); //Substring replacement
 echo"String after replacement (I by WE): $string3";
 echo"<br/></li>";
 echo"<br/><li>";
 $string4=strrev($string1);  //String reversal
 echo"String Reversal: $string4";
 echo"<br/></li>";
 echo"<br/><li>";
 $string5 =rtrim("I LOVE       ");    //Removing blank spaces from right of the string
 $string6=ltrim("       2K8CSE.");
 echo $string5.$string6."(removing blank spaces)";
 echo"<br/></li>"; 

 echo "<br/><b>";
 echo"WWW.2K8618.BLOGSPOT.COM";
 echo"</b>";
?>


</body>
</html>
Output:







Substring Search & Replacement - UDP - Client Server Program - Network-DBMS Lab - C Program

Program:

// Server : sbs.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>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,ack=0,length=0,i,j,bi1=0;
    char buffer[100],substring[20],newstring[20];
    FILE *ptr,*op;
   
    s=socket(AF_INET,SOCK_DGRAM,0);
    server.sin_family=AF_INET;
    server.sin_port=2000;
    server.sin_addr.s_addr=inet_addr("127.0.0.1");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
   
   
   
    recvfrom(s,substring,sizeof(substring),0,(struct sockaddr *)&client,&sz);
    printf("\nSubstring received: %s.",substring);
    recvfrom(s,newstring,sizeof(newstring),0,(struct sockaddr *)&client,&sz);
    printf("\nNewstring received: %s",newstring);
   ptr=fopen("testrecord.txt","r");
    op=fopen("result.txt","w");
    while((buffer[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buffer[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buffer[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            fprintf(op,"%s",buffer);
           
        }
        else
        {
            fprintf(op,"%s",newstring);printf("\nString replaced.");
        }
        bi1=0;bzero(buffer,100);
       
    }
   
    sendto(s,&ack,sizeof(int),0,(struct sockaddr *)&client,sz);

  if(ack)
  {
  remove("testrecord.txt");
  rename("result.txt","testrecord.txt");
         printf("\nSaved Successfully.\n");
 }
 remove("result.txt");

 fclose(ptr);
 fclose(op);
 close(s);
}


// Client: sbc.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>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],newstring[20],substring[20];
   
   
    s=socket(AF_INET,SOCK_DGRAM,0);
    client.sin_family=AF_INET;
    client.sin_port=2000;
    client.sin_addr.s_addr=inet_addr("127.0.0.1");
   
   
    printf("Enter the substring: ");
    scanf("%s",substring);
    printf("\nNewstring: ");
       scanf("%s",newstring);
    sendto(s,substring,sizeof(substring),0,(struct sockaddr *)&client,sizeof(client));
   sendto(s,newstring,sizeof(newstring),0,(struct sockaddr *)&client,sizeof(client));

 recvfrom(s,&found,sizeof(int),0,NULL,NULL);
    if(found)
    {
        printf("\nSubstring found & replaced.\n");
    }
    else
    {
            printf("\nNot found.\n");

    }
    close(s);

}




// testrecord.txt

i like www.2k8618.blogspot.com.

Output:

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

Substring received: 2k8618.blogspot.com.
Newstring received: 2k8cse.cu.cc
Substring found.
String replaced.
Saved Successfully.
nn@linuxmint ~ $

Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbc.c -o client
nn@linuxmint ~ $ ./client
Enter the substring: 2k8618.blogspot.com

Newstring: 2k8cse.cu.cc

Substring found & replaced.
nn@linuxmint ~ $

// testrecord.txt

i like www.2k8cse.cu.cc.

Substring Search & Replacement 2 - Client Server Program - TCP - Network-DBMS Lab - C Program

Program:

// Server: sbstrs.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>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,ack=0,length=0,i,j,bi1=0;
    char buffer[100],substring[20],newstring[20];
    FILE *ptr,*op;
   
    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");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
    listen(s,1);
    sock=accept(s,(struct sockaddr *)&client,&sz);
   
    recv(sock,substring,sizeof(substring),0);
    printf("\nSubstring received: %s.",substring);
    recv(sock,newstring,sizeof(newstring),0);
    printf("\nNewstring received: %s",newstring);
   
    ptr=fopen("testrecord.txt","r");
    op=fopen("result.txt","w");
    while((buffer[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buffer[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buffer[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            fprintf(op,"%s",buffer);
           
        }
        else
        {
            fprintf(op,"%s",newstring);printf("\nString replaced.");
        }
        bi1=0;bzero(buffer,100);
       
    }
   
    send(sock,&ack,sizeof(int),0);
    fclose(ptr);
    fclose(op);
    remove("testrecord.txt");
        rename("result.txt","testrecord.txt");
        remove("result.txt");
        printf("\nSaved Successfully.\n");
    close(sock);
    close(s);
}

// Client: sbstrc.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>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],string[20],substring[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));
   
    printf("Enter a string to search in server:");
    scanf("%s",string);
    send(s,&string,sizeof(string),0);
    recv(s,&found,sizeof(int),0);
    if(found)
    {
        printf("\nThe record found.");
    }
    else
    {
            printf("\nNot found...\n");
            exit(0);
    }
   
    printf("\nDo you want to replace it ?\nEnter 1 -> Yes 2 -> No\n");
    scanf("%d",&choice);
    send(s,&choice,sizeof(int),0);
    if(choice)
    {
        printf("\nSubstring: ");
        scanf("%s",substring);
        send(s,&substring,sizeof(substring),0);
        recv(s,&ack,sizeof(int),0);
        if(ack)
        {
                printf("\nSuccess... ");
        }
        else
        {
                printf("\nError... ");
        }
    }
    else
    {
        printf("\nThanks... ");
            exit(0);
    }
    close(s);

}

// testrecord.txt
    2K8CSE
2K861
2K862
2K863
2K864
2K865
2K866
2K867
2K868
2K869
2K8610
2K8611
2K8612
2K8613
2K8614
2K8615
2K8616
2K8617
2K8618
2K8619
2K8620
2K8621
2K8622
2K8623
2K8624
2K8625
2K8626
2K8627
2K8628
2K8629
2K8630
2K8631
2K8632
2K8633
2K8634
2K8635
2K8636
2K8637
2K8638

Output:

Terminal 1: (server)
nn@linuxmint ~ $ gcc sbstrs.c -o server
nn@linuxmint ~ $ ./server

Substring received: 2K8618.
Newstring received: NIDHEESH
Substring found.
String replaced.
Saved Successfully.
nn@linuxmint ~ $

Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbstrc.c -o client
nn@linuxmint ~ $ ./client
Enter the Substring: 2K8618

Enter the Newstring: NIDHEESH

Substring found & Replaced...
nn@linuxmint ~ $

// testrecord.txt

    2K8CSE
2K861
2K862
2K863
2K864
2K865
2K866
2K867
2K868
2K869
2K8610
2K8611
2K8612
2K8613
2K8614
2K8615
2K8616
2K8617
NIDHEESH
2K8619
2K8620
2K8621
2K8622
2K8623
2K8624
2K8625
2K8626
2K8627
2K8628
2K8629
2K8630
2K8631
2K8632
2K8633
2K8634
2K8635
2K8636
2K8637
2K8638

Substring Search & Replacement 1 - Client Server Program - Network-DBMS Lab - C Program

Program:

// Server: sbstrs1.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>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,ack=0,length=0,i,j,bi2=0,bi1=0;
    char buf1[200],buf2[200],substring[20],newstring[20];
    FILE *ptr,*temp;
   
    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");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
    listen(s,1);
    sock=accept(s,(struct sockaddr *)&client,&sz);
   
    recv(sock,substring,sizeof(substring),0);
    printf("\nSubstring received: %s.",substring);
    recv(sock,newstring,sizeof(newstring),0);
    printf("\nNewstring received: %s.",newstring);
   
    ptr=fopen("testrecord.txt","r");
    while((buf1[bi1]=getc(ptr))!=EOF)
        {
                   found=0;
                   bi1++;
                   if(buf1[bi1-1]==substring[0])
                   {
                       length=1;
                       for(i=1;i<strlen(substring);i++)
                    {
                        if((buf1[bi1]=getc(ptr))==substring[i])
                        {                           
                            length++;
                            bi1++;
                        }
                        else
                {
                    bi1++;
                    break;
                }
                    }
                    if(length==strlen(substring))
                    {
                        found=1;
                        ack=1;
                        printf("\nSubstring found.");
            }
        }
        if(!found)
        {
            for(i=0;i<bi1;i++)
            {
                buf2[bi2]=buf1[i];
                bi2++;
            }
           
        }
        else
        {
            for(i=0;i<=strlen(newstring)-1;i++)
                    {
                        buf2[bi2]=newstring[i];
                bi2++;
            }
            printf("\nReplaced.\n");
        }
        bi1=0;
    }
    send(sock,&ack,sizeof(int),0);
    fclose(ptr);
    temp=fopen("temp.txt","w");
           fprintf(temp,"%s",buf2);
    fclose(temp);
    remove("testrecord.txt");
        rename("temp.txt","testrecord.txt");
        remove("temp.txt");
    close(sock);
    close(s);
}


// Client: sbstrc.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>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],substring[20],newstring[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));
   
    printf("Enter the Substring: ");
    scanf("%s",substring);
    printf("\nEnter the Newstring: ");
    scanf("%s",newstring);
    send(s,&substring,sizeof(substring),0);

    send(s,&newstring,sizeof(newstring),0);
   
    recv(s,&ack,sizeof(int),0);

    if(ack)
    {
        printf("\nSubstring found & Replaced...\n");
    }
    else
    {
            printf("\nNot found...\n");
    }
    close(s);

}

// testrecord.txt

hi, goodnight...
ok.

Output:

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

Substring received: night.
Newstring received: morning.
Substring found.
Replaced.
nn@linuxmint ~ $

// Terminal 2: (Client)
nn@linuxmint ~ $ gcc sbstrc.c -o client
nn@linuxmint ~ $ ./client
Enter the Substring: night

Enter the Newstring: morning

Substring found & Replaced...
nn@linuxmint ~ $


// testrecord.txt
hi, goodmorning...
ok.

String Search and Replacement - Client-Server Program - Network-DBMS Lab - C Program

Program:

// Server : strs.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>
#include<stdlib.h>


main ()
{
    struct sockaddr_in client,server;
    int s,sock,found,sz,choice=0,ack=0;
    char buffer[20],string[20],substring[20];
    FILE *fp,*temp;
   
    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");
    sz=sizeof(client);
    bind(s,(struct sockaddr *)&server,sizeof(server));
    listen(s,1);
    sock=accept(s,(struct sockaddr *)&client,&sz);
    recv(sock,string,sizeof(string),0);
    printf("String recieved: %s",string);
    found=0;
    if((fp=fopen("testrecord.txt","r"))==NULL)
    {
        printf("! The file is empty...\n");
        send(sock,&found,sizeof(int),0);
    }
    else
    {
        while(!feof(fp)&&found==0)
                {
                    fscanf(fp,"%s",buffer);
                    if(strcmp(buffer,string)==0)
                    {
                        found=1;
                printf("\nThe record found.\n");
            }
        }
        send(sock,&found,sizeof(int),0);
        fclose(fp);
    }
   
   
    recv(sock,&choice,sizeof(int),0);
    if(choice)
    {
       
        recv(sock,substring,sizeof(substring),0);
        printf("\nString Replacement:\nSubstring: %s",substring);
        temp=fopen("temp.txt","w");
        if((fp=fopen("testrecord.txt","r"))==NULL)
        {
            printf("! The file is empty...\n");
            send(sock,&ack,sizeof(int),0);
        }
        else
        {
           
            while(!feof(fp))
                {
                    fscanf(fp,"%s",buffer);
                           if(strcmp(buffer,string)==0)
                           {
                               fprintf(temp,"%s ",substring);
                    ack=1;                                               
                }
                else
                {
                    fprintf(temp,"%s ",buffer);
                }
            }
            send(sock,&ack,sizeof(int),0);
        }
        fclose(fp);
        fclose(temp);
        remove("testrecord.txt");
                rename("temp.txt","testrecord.txt");
                remove("temp.txt");
    }
    else
    {
        printf("\nNo replacement... \n");
            exit(0);
    }   
   
    close(sock);
    close(s);
}

// Client: strc.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>
#include<stdlib.h>

main()
{
    struct sockaddr_in client;
    int s,found=0,choice=0,ack=0;;
    char buffer[20],string[20],substring[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));
   
    printf("Enter a string to search in server:");
    scanf("%s",string);
    send(s,&string,sizeof(string),0);
    recv(s,&found,sizeof(int),0);
    if(found)
    {
        printf("\nThe record found.");
    }
    else
    {
            printf("\nNot found...\n");
            exit(0);
    }
   
    printf("\nDo you want to replace it ?\nEnter 1 -> Yes 2 -> No\n");
    scanf("%d",&choice);
    send(s,&choice,sizeof(int),0);
    if(choice)
    {
        printf("\nSubstring: ");
        scanf("%s",substring);
        send(s,&substring,sizeof(substring),0);
        recv(s,&ack,sizeof(int),0);
        if(ack)
        {
                printf("\nSuccess... \n");
        }
        else
        {
                printf("\nError... \n");
        }
    }
    else
    {
        printf("\nThanks... ");
            exit(0);
    }
    close(s);

}

// testrecord.txt

BAJAJ YAMAHA HONDA HEROHONDA TVS SUZUKI

Output:

Terminal 1: (Server)
nn@linuxmint ~ $ gcc strs.c -o server
nn@linuxmint ~ $ ./server
String recieved: HEROHONDA
The record found.

String Replacement:
Substring: HEROnn@linuxmint ~ $

Terminal 2: (Client)
nn@linuxmint ~ $ gcc strc.c -o client
nn@linuxmint ~ $ ./client
Enter a string to search in server:HEROHONDA

The record found.
Do you want to replace it ?
Enter 1 -> Yes 2 -> No
1

Substring: HERO

Success... nn@linuxmint ~ $

// testrecord.txt

BAJAJ YAMAHA HONDA HERO TVS SUZUKI

Shared Memory - String - Systems Lab - C Program


Program:

#include<stdio.h>
#include<unistd.h>
#include<sys/ipc.h>
#include<sys/shm.h>
#include<sys/wait.h>
#include<sys/types.h>
#include"string.h"
#define shmsize 100
#define shmmode (SHM_R|SHM_W|IPC_CREAT|IPC_EXCL)
#define shmkey (key_t)31
int size;
int main()
{
    int shmid1, shmid2, pid, status;
   
    int *shmdata1, *shmdata2, *shmdata;
   
    char str1[20],str2[20],str3[20];
    int i,j,k,size2,size3;
    struct shmid_ds *shmidds;
    shmid1 = shmget(shmkey,shmsize,shmmode);
    shmdata1 = (int*)shmat(shmid1,0,0);
    shmdata = shmdata1;
    printf("Enter the string:");
    scanf("%s",str1);
    size=strlen(str1);
    for(j=0;j<size;j++)
    {
        *shmdata1=str1[j];
        shmdata1=shmdata1+sizeof(char);
    }
    pid = fork();           
    if(pid == 0)
    {
        printf("Enter the string:");
        scanf("%s",str2);
        size2=strlen(str2);
        for(i=0;i<size2;i++)
        {
            *shmdata1=str2[i];
            shmdata1+=sizeof(char);
        }
        size=size+size2;
    }
    while((pid = wait(&status))!= -1);
    shmdata1=shmdata;
    for(i=0;i<size;i++)
    {
        str3[i]=*shmdata1;
        shmdata1+=sizeof(char);
    }
    str3[size]='\0';
    printf("%s\n\n\n\n\n\n",str3);
shmdt((void*)shmdata);
shmdt((void*)shmdata1);
shmctl(shmid1,IPC_RMID,shmidds);
    return 1;
}

Output:
nn@ubuntu:~$ gcc shmstr.c
nn@ubuntu:~$ ./a.out
Enter the string:hai
Enter the string:helo
haihelo





hai





nn@ubuntu:~$

String Sort - LISP

Program:

Download Runnable Code

;;BUBBLESORT (STRING)

(defun breadn(n)
(setf arr (make-array n))
(format t "Enter the strings:~&")
(dotimes (x n t)
(setf (aref arr x) (read))))


(defun bprintn(n)
(dotimes(x n t)
    (print (aref arr x))))
(defun swap(x y)
    (setf temp (aref arr x))
    (setf (aref arr x) (aref arr y))
    (setf (aref arr y) temp)
)

(defun bsortn ( n )
    (breadn n);;(bprintn n)
    (do (( i 1 (+ i 1)))
    ((= i n))
    (setf k (- n 1))
    (do((j 0 (+ j 1    )))
    ((= j k))
    (setf m (+ j 1))
    (if (string> (aref arr j) (aref arr m))
        (swap j m))
))
    (bprintn n)
)
Output:
Break 2 [4]> (load 'bbs.lsp)
;; Loading file bbs.lsp ...
;; Loaded file bbs.lsp
T
Break 2 [4]> (bsortn 5)
Enter the strings:
orange
guava
apple
banana
mango

APPLE
BANANA
GUAVA
MANGO
ORANGE
T
Break 2 [4]>

Download

FIND REVERSE OF EACH WORD IN A STRING - Assembly Language - MASM

Program:

        ;FIND REVERSE OF EACH WORD IN A STRING


.model small
.stack 200h
.data
msg1 db 10,13,'Enter the string:$'
msg2 db 10,13,'Reverse of the string:$'
newline db 10,13,' $'
buff db 60 dup(?)
flag db 0
stackempty db 1
.code
print macro msg
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 buff
mov cx,0
mov bx,0
nxtchar:
mov ah,01h
int 21h
inc bx
cmp al,' '
je popy
cmp al,0dh
je popy
mov ah,0
push ax
mov stackempty,0
inc cx
jmp nxtchar
popy:
cmp stackempty,1
je stackkaali
cmp al,0dh
jne noflag
mov flag,1
noflag:
pop ax
mov [si],al
inc si
loop noflag
inc si
mov stackempty,1
cmp flag,1
je display
jmp nxtchar
stackkaali:
inc si
jmp nxtchar
display:
print msg2
print newline
mov si,offset buff
mov cx,bx
dispnxt:mov dh,0
mov dl,[si]
mov ah,2
int 21h
inc si
loop dispnxt
.exit
End
OUTPUT
F:\>rev
Enter the string:
kerala is called gods own country
Reverse of the string:
alarek si dellac sdog nwo yrtnuoc

Reverse of a String - Assembly Language - MASM


REVERSE OF A STRING

Program:
.model small
.stack 200h
.data
   msg1 db 10,13,"Enter string:",10,13,'$'
   msg2 db 10,13,"Reverse of the string is:",10,13,'$'
   arr db 20 dup(?)
.code
   print MACRO msg
   PUSH AX
   PUSH DX
   MOV AH,09H
   MOV DX,offset msg
   INT 21H
   POP DX
   POP AX
   ENDM
.startup
   print msg1
   mov arr,18
   mov dx,offset arr
   mov ah,0ah
   int 21h
   lea dx,msg2
   mov ah,09h
   int 21h
   mov ch,0h
   mov cl,arr[1]
   mov di,cx
   inc di
l1:
   mov dl,arr[di]
   mov ah,02h
   int 21h
   dec di
   loop l1
  .exit
End

Output:
F:\>revv
Enter string:
PALINDROME
Reverse of the string is:
EMORDNILAP

String Manipulation - Substring Search & Replacement C Program

Program:
#include<stdio.h>
#include<string.h>
main()
{
   char str[40],newstr[20],substr[20];
   int len1,len2,i,j,k,t=0,len3,d=0;
   printf("Enter the string:");
   gets(str);
   printf("Enter the substring:");
   gets(substr);
   printf("Enter the new string:");
   gets(newstr);
   len1 = strlen(str);
   len2 = strlen(substr);
   len3 = strlen(newstr);
   for(i=0;i<len1;i++)
   {
     for(j=t=0,k=i;j<len2;j++,k++)
     {
        if(str[k]==substr[j])
            t++;
        else
          break;
     }
     if(t==len2)
         break;
   }
   if(t==len2)
   {
     printf("\nThe substring is found.\n");
     printf("String replaced...\n");
     if(len3>len2)
     {
       for(j=len1;j>=k;j--)
       str[j+len3-len2] = str[j];
     }
     else
     {
       for(j=k;j<=len1;j++)
       str[j-(len2-len3)] = str[j];
     }
     while(newstr[d]!='\0')
     {
       str[i] = newstr[d];
       i++;
       d++;
     }
     puts(str);
   }
   else
   {
     printf("\nSubstring not found.\n");
   }
}

Output:
nn@linuxmint ~ $ gcc c18.c
/tmp/ccqnIqKS.o: In function `main':
c18.c:(.text+0x40): warning: the `gets' function is dangerous and should not be used.
nn@linuxmint ~ $ ./a.out
Enter the string:good night
Enter the substring:night
Enter the new string:morning

The substring is found.
String replaced...
good morning
nn@linuxmint ~ $
Related Posts Plugin for WordPress, Blogger...