Showing posts with label C. Show all posts
Showing posts with label C. Show all posts

Oracle Recruitment 2012 - Aptitude Questions - Basic Computer Knowledge

Oracle Financial Services Campus Recruitment - 2012

Aptitude Questions
 
BASIC COMPUTER KNOWLEDGE – Sample Questions

C - Aptitude Questions
This is the Basic level of the C assessment. No points will be deducted for wrong answers. 


Question Number 1
Which among the following are TRUE?
I. User has to explicitly define the numeric values of enumerations.
II. User has a control over the size of enumeration variables.
III. Enumerations cannot take negative values.
IV. Enumerations can have an effect local to the block if desired.
a. I and II
b. IV only
c. III and IV
d. II and III


Question Number 2
What will be the output of the following program?
struct emp
{
int length;
char* name;
};
int main()
{
char *vName = "Joseph";
struct emp *eVar = (struct emp*)malloc(sizeof(struct emp));
eVar->length = strlen(vName);
eVar->name = (char*)malloc(eVar->length+1);
strcpy(eVar->name, vName);
printf("%d %s\n", eVar->length, eVar->name);
free(eVar->name);
free(eVar);
return 0;
}
a. 6 Joseph
b. Compiler error
c. JunkValue Joseph
d. Runtime error


Question Number 3
Sizes of integral types are defined in which of the following headers?
a. ctype.h
b. stdarg.h
c. limits.h
d. cmath.h

Question Number 4
What will be the output of the following program?
int main()
{
union x
{
char v1;
short int v2;
};
union x x1;
x1.v2 = 1024;
printf("%d\n",(x1.v1 & 0xff00));
return 0;
}

a. Compiler error
b. 1024
c. 0
d. 1064


Question Number 5
What will be the output of the following program?
main()
{
int Oxygen=5;
printf("%d\n%d\n%d",Oxygen,Oxygen<<5,Oxygen>> 5);
return 0;
}
a. 5
160
 5
b. 5
160
0
c. 0
160
5
d. 5
128
0

Question Number 6
The __________ modifier hints to the compiler that the variable will be heavily used and should be kept in the CPU's registers.
a. Static
b. Register
c. Auto
d. Volatile


Question Number 7
What could be the correct reason for the below program to fail upon compilation?
Sum(int a, int b)
{
int a;
a = 57;
a = a + b;
return a;
}
I. Invalid arguments to the function.
II. Re-declaration of a.
III. Function must have a return type specified.

a. II only
b. II and III
c. I only
d. III only


Question Number 8
What will be the output of the following program?
void main()
{
printf("Value : %d",10^9);
}

a. Value : 1
b. Value : 3
c. Error
d. None of the above 

Question Number 9
What will be the output of the following program?
#include<stdio.h>
int main()
{
int ivar;
for(ivar=1;ivar<4,ivar++)
switch(ivar)
case 1: printf("%d",ivar);break;
{
case 2:printf("%d",ivar);break;
case 3:printf("%d",ivar);break;
}
switch(ivar)
case 4:printf("%d",ivar);
return 0;
a. 1 2 3 4
b. No output
c. 1 2 3
d. Compiler error


Question Number 10
What will be the output of the following program?
#include<stdio.h>
void Test(int i)
{
printf( "int ");
}
void Test(int &i)
{
printf( "int &");
}
void main()
{
int x=10;
Test(x);

 
a. Compiler error
b. int
c. Runtime error
d. int &


Question Number 11
What will be the output of the following program?
int main()
{
int i=1;
for( ; i < 5; i++);
printf("%d ", i);
return 0;
}

a. 1 2 3 4
b. 1
c. 1 2 3 4 5
d. 5


Question Number 1 2
What will tempStr contain after execution of the following program assuming that file.txt contains "abcdefg"?
int main()
{
char tmpStr[80];
FILE* fp = NULL;
fp = fopen("file.txt","rb");
fseek(fp, 4, SEEK_SET);
fgets(tmpStr, sizeof(tmpStr), fp);
return 0;
}

a. abcdefg
b. NULL
c. efg
d. efg followed by junk characters


Question Number 13
What will be the output of the following program?
#include<stdio.h>
int main()
{
if("lakeside"=="lakeside")
printf("Equal");
else
printf("Not equal");
return 0;
a. Not equal
b. Equal
c. Compiler error
d. None of the above

Question Number 14
Which among the following statements are TRUE?
I. The expression num[1] designates the very first element in the array.
II. It is necessary to initialize the array at the time of declaration.
III. The declaration num[SIZE] is allowed if SIZE is a macro.

a. I and II
b. I and III
c. III only
d. II and III


Question Number 15
What will be the output of the following program assuming file.txt contains "how are you"?
int main()
{
char ch; FILE* fp;
fp = fopen("file.txt","r");
while((ch=getc(fp)) != NULL)
printf("%c", ch);
fclose(fp);
}

a. Compiler error
b. 'how are you' followed by infinite loop
c. Does not print anything and stays in while loop
d. how are you


Question Number 16
Which of the following is TRUE about void pointer?
I. Void pointer has no datatype declared with it and it can be used to point variable of any datatype.
II. Void pointers does not occupy any memory.
III. Void pointers points to nothing. 
a. I only
b. I and III
c. II and III
d. I, II, and III


Question Number 17
Which of the following is TRUE about fseek(fileptr,0,SEEK_SET)?
a. Sets the file position marker to the middle of the file
b. Sets the file position marker to the end of the file
c. Its not a valid option
d. Sets the file position marker to the starting of the file


Question Number 18
Which of the following statements is/are FALSE?
I. Arrays can be passed by reference to a function.
II. Arrays can be passed by value to a function.
III. In pass by value the maximum array size cannot be more than 8KB. 
a. I and II
b. II and III
c. I, II, and III
d. II only


Question Number 19
Which among the following statements are TRUE?
I. stderr, stdin and stdout are FILE pointers.
II. Offset used in fseek() function call can be a negative number.
III. In a call to printf() function, the format specifier, %b , can be used to print binary equivalent of an integer.

a. II and III
b. I and II
c. III only
d. I and III


Question Number 20
Which of the following statements, is correct?
a. strcmp ( s1 , s2 ) returns a number less then 0 if s1 > s2
b. strcmp ( s1 , s2 ) returns 0 if s1 == s2
c. strcmp ( s1 , s2 ) returns a number greater then 0 if s1 < s2
d. strcmp ( s1 , s2 ) returns 1 if s1 == s2

Matrix Multiplication - TCP - Client Server Program - Network-DBMS Lab - C Program

Program:

// Client Program: mc.c

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

main()
{
 struct sockaddr_in client,server;
 int a[10][10],b[10][10],c[10][10],mt[4];
 int s,sock,r,i,j;
 client.sin_family=AF_INET;
 client.sin_port=3000;
 client.sin_addr.s_addr=inet_addr("127.0.0.1");
 s=socket(AF_INET,SOCK_STREAM,0);
 connect(s,(struct sockaddr *)&client,sizeof(client));
 
 printf("Enter order of matrix 1:");
  scanf("%d %d",&mt[0],&mt[1]);
 printf("Enter order of matrix 2:");
  scanf("%d %d",&mt[2],&mt[3]);
 if(mt[1]!=mt[2])
  printf("Matrices cannot be multiplied");
 else
 {
  send(s,&mt,sizeof(mt),0);
  printf("Enter Elements for matrix 1:\n");
  for(i=0;i<mt[0];i++)
  {
   for(j=0;j<mt[1];j++)
   {
    scanf("%d",&a[i][j]);
   }
  }
  send(s,&a,sizeof(a),0);
  printf("Enter Elements for matrix 2:\n");
  for(i=0;i<mt[2];i++)
  {
   for(j=0;j<mt[3];j++)
   {
    scanf("%d",&b[i][j]);
   }
  } 
  send(s,&b,sizeof(b),0);
  
  recv(s,&c,sizeof(c),0);
  printf("\nResult:\n"); 
  for(i=0;i<mt[0];i++)
  {
   printf("\n");  
   for(j=0;j<mt[3];j++)
   {
    printf(" %d ",c[i][j]);
   }
  } 
 }
 printf("\n"); 
 close(s);
}




Server Program: ms.c

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

main()
{
 struct sockaddr_in client,server;
 int a[10][10],b[10][10],c[10][10],mt[4];
 int s,sock,r,i,j,sz,k;
 server.sin_family=AF_INET;
 server.sin_port=3000;
 server.sin_addr.s_addr=inet_addr("127.0.0.1");
 s=socket(AF_INET,SOCK_STREAM,0);
 bind(s,(struct sockaddr *)&server,sizeof(server));
 listen(s,1);
 sz=sizeof(server);
 sock=accept(s,(struct sockaddr *)&server,&sz);
 recv(sock,&mt,sizeof(mt),0);
 recv(sock,&a,sizeof(a),0);
 printf("\nReceived Matrix 1\n");
 for(i=0;i<mt[0];i++)
 {
  printf("\n");  
  for(j=0;j<mt[1];j++)
  {
   printf(" %d ",a[i][j]);
  }
 } 
  
 recv(sock,&b,sizeof(b),0);
 printf("\nReceived Matrix 2\n"); 
 for(i=0;i<mt[2];i++)
 {
  printf("\n");  
  for(j=0;j<mt[3];j++)
  {
   printf(" %d ",b[i][j]);
  }
 } 
  
 printf("\nMultiplying........\n"); 
 for(i=0;i<mt[0];i++)
 for(j=0;j<mt[3];j++)
 {
  c[i][j]=0;
  for(k=0;k<mt[1];k++)
   c[i][j]+=a[i][k]*b[k][j];
 }
 printf("\nResult:\n") ; 
 for(i=0;i<mt[0];i++)
 {
  printf("\n");  
  for(j=0;j<mt[3];j++)
  {
   printf(" %d ",c[i][j]);
  }
 }
 printf("\n"); 
 send(sock,&c,sizeof(c),0);
 close(sock);
 close(s); 
}



Output:

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

Received Matrix 1

1 1 
1 1 
Received Matrix 2

2 2
2 2
Multiplying........

Result:

4 4 
4 4 
nn@linuxmint ~ $ 

Terminal 2: (Client)
nn@linuxmint ~ $ gcc mc.c -o c
nn@linuxmint ~ $ ./c
Enter order of matrix 1:2
2
Enter order of matrix 2:2
2
Enter Elements for matrix 1:
1
1
1
1
Enter Elements for matrix 2:
2
2
2
2

Result:

4 4 
4 4
nn@linuxmint ~ $ 

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

2K8CSE - Generate Index in a FIle - C Program

Program:

// gen.c

#include<stdio.h>

main ()
{

    int i=1;
    FILE *temp;
    temp=fopen("testrecord.txt","w");
    fprintf(temp,"\t2K8CSE\n");
    while(i<39)
    {
        fprintf(temp,"2K86%d\n",i);
        i++;
    }
    fclose(temp);   
}

Output:
nn@linuxmint ~ $ gcc gen.c
nn@linuxmint ~ $ ./a.out
nn@linuxmint ~ $

// 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

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

Student Record Management System - Client-Server Program - Network-DBMS Lab - C program

Program:

// Client Program: sttcpc.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>

struct
{
    int roll_no;
    char name[20];
    int mark;
}student;
       
main()
{
   struct sockaddr_in client;
   int s,flag,choice,shift,rollnumber,found,continu;
   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);
  
   do{
      printf("\n\t\t<<< STUDENT FILE MANAGEMENT SYSTEM>>>\n\t1 -> Store a new record in server\n\t2 -> Search a student record from server\nEnter your choice: ");
      scanf("%d",&choice);
      send(s,&choice,sizeof(int),0);
      switch(choice)
      {
       case  1:
       printf("\nEnter Student Details:\nRoll number (Integer): ");
       scanf("%d",&student.roll_no);
       printf("\nName : ");
       scanf("%s",student.name);
       printf("\nMark : ");
       scanf("%d",&student.mark);
       send(s,(const char*)&student,sizeof(student),0);
       printf(" A student record has been sent successfully...\n");
       printf("\n press 1 -> conitnue,\n\t 2 -> Exit: ");
       scanf("%d",&shift);
       send(s,&shift,sizeof(int),0);
      
       if(shift==1)
        break;
       else if(shift==2)
        {
            continu=0;
            break;
        }
               
       case 2:
       printf("\nEnter Roll number: ");
       scanf("%d",&rollnumber);
       send(s,&rollnumber,sizeof(int),0);
       recv(s,&found,sizeof(int),0);
       if(found)
       {
          recv(s,(void *)&student,sizeof(student),0);
         printf("\nThe record is found.\nRoll no: %d\nName: %s\nMark: %d \n",student.roll_no,student.name,student.mark);
       }
       else
       {
         printf("Not found...\n");
         exit(0);
       }
       printf("\n press 1 -> conitnue,\n\t 2 -> Exit: ");
       scanf("%d",&shift);
       send(s,&shift,sizeof(int),0);
       if(shift==1)
        break;
       else if(shift==2)
       {
            continu=0;
            break;
       }
       default :
          printf(" Bad choice...Exiting...\n");
          continu=0;
        break;
        }
      }while(continu!=0);
           
      close(s);
}

// Server Program: sttcps.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>

struct
{
    int roll_no;
    char name[20];
    int mark;
}student;

main()
{
  int s,n,sock,choice,found,rollnumber,shift,continu;
  char buffer[20];
  FILE *fp;
  struct sockaddr_in client,server;
  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);
  printf("\n\t\t<<< STUDENT FILE MANAGEMENT SYSTEM>>>\n");
  do{
  recv(sock,&choice,sizeof(int),0);
  switch(choice)
  {
    case 1 :
   
        recv(sock,(void *)&student,sizeof(student),0);
        printf("\nNew student record received is:%d %s %d \n",student.roll_no,student.name,student.mark);
        fp=fopen("studentfile.txt","a+");
        fprintf(fp,"\n%d\t%s\t%d\t",student.roll_no,student.name,student.mark);
        fclose(fp);
        printf(" A student record has been added successfully...\n");
        recv(sock,&shift,sizeof(int),0);
        if(shift==1)
        break;
        else if(shift==2)
        {
                continu=0;
                break;
        }
      
    case 2:
           recv(sock,&rollnumber,sizeof(int),0);
           printf("Searching record with rollnumber=%d.\n",rollnumber);
           found=0;
           if((fp=fopen("studentfile.txt","r"))==NULL)
        {
            printf(" ! The File is Empty...\n\n");
            send(sock,&found,sizeof(int),0);
        }
        else
        {
            while(!feof(fp)&& found==0)
                {
                fscanf(fp,"\n%d\t%s\t%d\t",&student.roll_no,student.name,&student.mark);
                if(student.roll_no==rollnumber)
                    found=1;
            }
            send(sock,&found,sizeof(int),0);
                 fclose(fp);
                 if(found)
            {
                printf(" Record found...\n");
                send(sock,(const char*)&student,sizeof(student),0);
               
            }
            else
            {
                printf(" Record not found...Exiting\n");
                exit(0);
            }   
           
        }
        recv(sock,&shift,sizeof(int),0);
        if(shift==1)
        break;
        else if(shift==2)
        {
                continu=0;
                break;
        }
    default :
          printf(" Bad request...Exiting...\n");
          continu=0;
        break;
  }
  }while(continu!=0);
  
  close(sock);
  close(s);
}

Output:

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

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 1

Enter Student Details:
Roll number (Integer): 1

Name : nidheesh               

Mark : 45
 A student record has been sent successfully...

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 1

Enter Student Details:
Roll number (Integer): 2

Name : nithin

Mark : 45
 A student record has been sent successfully...

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 1

Enter Student Details:
Roll number (Integer): 3

Name : asish

Mark : 45
 A student record has been sent successfully...

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 2

Enter Roll number: 3

The record is found.
Roll no: 3
Name: asish
Mark: 45

 press 1 -> conitnue,
     2 -> Exit: 1

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>
    1 -> Store a new record in server
    2 -> Search a student record from server
Enter your choice: 2

Enter Roll number: 5
Not found...
nn@linuxmint ~ $

Terminal 2: (Server)

nn@linuxmint ~ $ gcc sttcps.c -o server
nn@linuxmint ~ $ ./server

        <<< STUDENT FILE MANAGEMENT SYSTEM>>>

New student record received is:1 nidheesh 45
 A student record has been added successfully...

New student record received is:2 nithin 45
 A student record has been added successfully...

New student record received is:3 asish 45
 A student record has been added successfully...
Searching record with rollnumber=3.
 Record found...
Searching record with rollnumber=5.
 Record not found...Exiting
nn@linuxmint ~ $

File : studentfile.txt



1    nidheesh    45   
2    nithin    45   
3    asish    45

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 ~ $


Related Posts Plugin for WordPress, Blogger...