Showing posts with label Name. Show all posts
Showing posts with label Name. Show all posts

How To Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.

Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.



  1. //Write a C program that reads your first and last names and then converts them to upper-case and lower-case letters. The results will be printed to standard output console.
  2. #include<string.h>
  3. #include<iostream>
  4. using namespace std;
  5. int main()
  6. {
  7.     char c1[20],c2[20];
  8.     int i;
  9.     cin>>c1;
  10.     cin>>c2;
  11.     for(i=0;i<strlen(c1);i++)
  12.         if(c1[i]>='a'&&c1[i]<='z')
  13.             c1[i]=c1[i]-32;
  14.         else
  15.             if(c1[i]>='A'&&c1[i]<='Z')
  16.                 c1[i]=c1[i]+32;
  17.     for(i=0;i<strlen(c2);i++)
  18.         if(c2[i]>='a'&&c2[i]<='z')
  19.             c2[i]=c2[i]-32;
  20.         else
  21.             if(c2[i]>='A'&&c2[i]<='Z')
  22.                 c2[i]=c2[i]+32;
  23.         cout<<c1<<" "<<c2;
  24. }

Client/Server C program to make client send the name of a file

Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.


Solution:

Client Side:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("Err:no port no.\nusage:\n./client portno\n ex:./client 7777\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
bzero((char *)&serv,sizeof(serv));
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_port=htons(portno);
if(connect(sockfd,(struct sockaddr *)&serv,sizeof(serv))<0)
{
printf("server not responding..\n\n\n\ti am to terminate\n");
exit(1);
}
printf("Enter the file with complete path\n");
scanf("%s",&buffer);
if(write(sockfd,buffer,strlen(buffer))<0)
printf("Err writing to socket..\n");
bzero(c,2000);
printf("Reading..\n..\n");
if(read(sockfd,c,1999)<0)
printf("error: read error\n");
printf("client: display content of %s\n..\n",buffer);
fputs(c,stdout);
printf("\n..\n");
return 0;
}

Server Side:

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netdb.h>
int main(int argc,char *argv[])
{
int sockfd,newsockfd,portno,len,n;
char buffer[256],c[2000],cc[20000];
struct sockaddr_in serv,cli;
FILE *fd;
if(argc<2)
{
printf("erroe:no port no\n usage:\n/server port no\n");
exit(1);
}
sockfd=socket(AF_INET,SOCK_STREAM,0);
portno=atoi(argv[1]);
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=INADDR_ANY;
serv.sin_port=htons(portno);
bind(sockfd,(struct sockaddr *)&serv,sizeof(serv));
listen(sockfd,10);
len=sizeof(cli);
printf("serve:\nwaiting for connection\n");
newsockfd=accept(sockfd,(struct sockaddr *)&cli,&len);
bzero(buffer,255);
n=read(newsockfd,buffer,255);
printf("\nserver recv:%s\n",buffer);
if((fd=fopen(buffer,"r"))!=NULL)
{
printf("server:%s found\n opening and reading..\n",buffer);
printf("reading..\n..reading complete");
fgets(cc,2000,fd);
while(!feof(fd))
{
fgets(c,2000,fd);
strcat(cc,c);
}
n=write(newsockfd,cc,strlen(cc));
if(n<0)
printf("error writing to socket");
printf("\ntransfer complete\n");
}
else
{
printf("server:file not found\n");
n=write(newsockfd,"file not foung",15);
if(n<0)
printf("error: writing to socket..\n");
}
return 0;
}


Output Using TCP or IP sockets write a client/server C program to make client send the name of a file and server to send back the contents of the requested file if present.:

Server part output


[root@localhost ~]# ./a.out 4455
server online
waiting for request...
server:(null) found!
transfering the contentserver:transfer completed
Segmentation fault
[root@localhost ~]#

client part output

[root@localhost ~]# ./a.out 4455
waiting for serverserver is online
client enter the path2.c
File Recieved:Display Contents#include<stdio.h>
struct frame{
int fslno;
char data[5];
}arr[10];
sort(int n)
{
struct frame temp;
int i,j,ex;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-i-1;j++)
if(arr[j].fslno>arr[j+1].fslno)
{
temp=arr[j];
ar

C Program to Recursively converts all file-names to lower-case

How to write a C Program to Recursively converts all file-names to lower-case in C Programming Language ?


Solution:


/*Recursively converts all file-names to lower-case.*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>

int verbose = 0;

int convertDirectory ( const char *name )
    /* returns number of converted file-names, -1 if failed */
{
    DIR *dir;
    char fn[2048], oldFn[2048], *ptr;
    int len, rw, counter, needs, subCounter;
    struct dirent *entry;
    struct stat st;

    if ( !name || !name[0] ) return -1;
    dir = opendir(name);
    if ( !dir ) return -1;
    strcpy(fn,name);
    len = strlen(fn);
    fn[len++] = '/';
    rw = 0;                                 /* the directory is writtable? */
    counter = 0;                            /* number of changed file-names */
    while ( (entry = readdir(dir)) ) {      /* process one directory entry */
        if ( entry->d_name[0] == '.' &&
             (!entry->d_name[1] ||
              entry->d_name[1] == '.' &&
              !entry->d_name[2]) )
            continue;                       /* special directory */
            /* assemble the item name: */
        strcpy(fn+len,entry->d_name);
            /* lower-case conversion: */
        needs = 0;
        for ( ptr = fn + len; *ptr; ptr++ )
            if ( isupper(*ptr) ) {
                needs = 1;
                break;
                }
            /* the directory-entry needs to be converted: */
        if ( needs ) {
            if ( !rw ) {                    /* make the mother directory writable */
                fn[len-1] = (char)0;
                if ( !stat(fn,&st) && !(st.st_mode & S_IWUSR) )
                    chmod(fn,st.st_mode|S_IWUSR);
                fn[len-1] = '/';
                rw = 1;
                }
            strcpy(oldFn,fn);
            for ( ptr = fn + len; *ptr; ptr++ )
                if ( isupper(*ptr) )
                    *ptr = tolower(*ptr);
            rename(oldFn,fn);
            if ( verbose ) printf("%s -> %s\n",oldFn,fn);
            counter++;
            }
            /* stat the entry .. subdirectories must be traversed */
        if ( !stat(fn,&st) &&
             S_ISDIR(st.st_mode) ) {        /* directory */
            subCounter = convertDirectory(fn);
            if ( subCounter >= 0 )
                counter += subCounter;
            else
                if ( verbose )
                    printf("Error converting '%s' directory\n",fn);
            }
        }
    closedir(dir);

    return counter;
}

int main ( int argc, char *argv[] )
{
    /* TODO: command-line argument: directory to convert */
    /* TODO: -v (verbose) */
    int converted = convertDirectory(".");
    if ( converted <= 0 )
        printf("No file-names were converted\n");
    else
        printf("%d file-names were converted\n",converted);
    return 0;
}

C Program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order

How to write a c program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order in C Programming Language ?


Solution:
/*C program to accept 5 names from user & store these names into an array. Sort these array elements in alphabetical order*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
  void main()
{
  char c[10][20],temp[20];
  int i,j,n;
  clrscr();
  printf("\nEnter number of strings: ");
  scanf("%d",&n);
  printf("\nEnter the strings: ");
  for(i=0;i<=n;i++)
{
  gets(c[i]);
}
  puts(c[i]);
  for(i=0;i<=n;i++)
{
  for(j=i+1;j<=n;j++)
{
  if(strcmp(c[i],c[j])>0)
 {
  strcpy(temp,c[i]);
  strcpy(c[i],c[j]);
  strcpy(c[j],temp);
 }
}
}
  printf("\nSorted list is:\n");
  for(i=0;i<=n;i++)
  printf("\n%s",c[i]);
  getch();
}

//OUTPUT:
//Enter the number of strings: 3
//Enter the strings: VINIT
//RAVI
//SURAJ
//Sorted list is: RAVI
//SURAJ
//VINIT