menu driven program to perform following operations Write separate function for each option -check if one string is substring of another string -count the number of occurance of character in the string -Exit

#include
#include
#include
void main()
{
void substring();
void occurrence();
while(1)
{
int ch;
clrscr();
printf(“\n 1:check substring\n”);
printf(“\n 2:occurrence \n”);
printf(“\n 3:EXIT”);
printf(“\n\n enter the choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:substring();getch();
break;
case 2:occurrence();getch();
break;
case 3:exit(0);
break;
}
}
}
void substring()
{
int s;
char ch[50],sub[10];
printf(“\n enter the string : “);
scanf(“%s”,ch);
printf(“\n enter the substring: “);
scanf(“%s”,sub);
s=substr(ch,sub);
if(s==-1)
printf(“\n substring not found”);
else
printf(“\n substring is found \n”);
printf(“\n starting location of substring :%d”,s);
}

int substr(char ch[50],char sub[10])
{
int b,e,l,i=0,j=0;
//l=strlen(sub);
while(ch[i]!=”)
{
if(ch[i]==sub[j])
{
b=i;
while(sub[j]!=”)
{
e=ch[i]-sub[j];
i++;
j++;
}
}
i++;
}
if(e==0)
return(b+1);
else
return(-1);
}

void occurrence()
{
char *s1,*s2,ch;
int cnt;
clrscr();
printf(“\n enter the string \n”);
scanf(“%s”,s1);
printf(“\n enter the character =”);
scanf(“%s”,s2);
cnt=0;
while(*s1!=”)
{
if(*s1==*s2)
{
cnt++;
}
s1++;
}
printf(“\n %s occour %d times “,s2,cnt);
}

c program to accept n numbers from user store these number into an array count number of occurrence of each number

#include
#include
void main()
{
int n,i,j,cnt,a[10],temp,no;
clrscr();
printf(“\nenter the limit = “);
scanf(“%d”,&n);
printf(“\n Enter the %d number”,n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
for(j=i+1;ja[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(i=0;i<n;i=j)
{
no=a[i];
cnt=1;
for(j=i+1;j<n;j++)
{
if(a[j]!=no)
break;
else
cnt++;
}
printf("\n%d no occurs %d times\n ",no,cnt);
}
getch();
}

‘C’ Program to accept ‘n’ numbers and store all prime numbers in an array and display this array

#include
#include
void main()
{
int a[50],i=0,j=0;
int n,f=0;
clrscr();
printf(“\n enter the limit =”);
scanf(“%d”,&n);
printf(“\n enter the elements \n”);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
for(i=0;i<n;i++)
{
f=0;
if(a[i]<=0)
break;
else
{
for(j=2;j<=a[i]/2;j++)
{
if(a[i]%j==0)
{
f=1;break;
}
}
if(f==0)
printf("\nprime number=%d",a[i]);
}
}
getch();
}

Delete File from using C programming

the file HELLO.txt. In case if the file will not be deleted, the function perror() prints the error message on the console.

Here i am using TurboC++ IDE and my source file is in the C:\TC\BIN directory and the file to be deleted ABC.txt should be first placed inside the same bin directory.

#include
#include
void main()
{
if(remove(“ABC.DAT”)==-1)
perror(“Error in Del file”);
else
printf(“File is remove”);
getch();
}

C program to convert the file contents in Upper-case & Write Contents in a output file

#include
#include

void main()
{
FILE *fp1,*fp2;
char a;
clrscr();

fp1=fopen(“A.txt”,”r”);
if(fp1==NULL)
{
puts(“cannot open this file”);
exit(1);
}

fp2=fopen(“B.txt”,”w”);
if(fp2==NULL)
{
puts(“Not able to open this file”);
fclose(fp1);
exit(1);
}

do
{
a=fgetc(fp1);
a=toupper(a);
fputc(a,fp2);
}while(a!=EOF);

fcloseall();
getch();
}

Open one file in the read mode another file in the write mode.
fp1=fopen(“test.txt”,”r”);
fp2=fopen(“test1.txt”,”w”);

Now read file character by character. toupper() function will convert lower case letter to upper case.

C program to Convert the file contents in Upper-case & Write Contents in a Output file
« Previous sub topic » Next sub topic
C program to convert the file contents in Upper-case & Write Contents in a output file

Write a program to read a text file and convert the file contents in capital (Upper-case) and write the contents in a output file. Program to copy the contents of one file into another by changing case.

#include
#include

void main()
{
FILE *fp1,*fp2;
char a;
clrscr();

fp1=fopen(“test.txt”,”r”);
if(fp1==NULL)
{
puts(“cannot open this file”);
exit(1);
}

fp2=fopen(“test1.txt”,”w”);
if(fp2==NULL)
{
puts(“Not able to open this file”);
fclose(fp1);
exit(1);
}

do
{
a=fgetc(fp1);
a=toupper(a);
fputc(a,fp2);
}while(a!=EOF);

fcloseall();
getch();
}

Explanation :

Open one file in the read mode another file in the write mode.

fp1=fopen(“test.txt”,”r”);
fp2=fopen(“test1.txt”,”w”);

Now read file character by character. toupper() function will convert lower case letter to upper case.

do {
a=fgetc(fp1);
a=toupper(a);
fputc(a,fp2);
}while(a!=EOF);

C Program to Compare two text/data files in C Programming

#include

int main()
{
FILE *fp1, *fp2;
int ch1, ch2;
char fname1[40], fname2[40] ;
clrscr();
printf(“Enter name of first file :”) ;
gets(fname1);

printf(“Enter name of second file:”);
gets(fname2);

fp1 = fopen( fname1, “r” );
fp2 = fopen( fname2, “r” ) ;

if ( fp1 == NULL )
{
printf(“Cannot open %s for reading “, fname1 );
exit(1);
}
else if (fp2 == NULL)
{
printf(“Cannot open %s for reading “, fname2 );
exit(1);
}
else
{
ch1 = getc( fp1 ) ;
ch2 = getc( fp2 ) ;

while( (ch1!=EOF) && (ch2!=EOF) && (ch1 == ch2))
{
ch1 = getc(fp1);
ch2 = getc(fp2) ;
}

if (ch1 == ch2)
printf(“Files are identical n”);
else if (ch1 != ch2)
printf(“Files are Not identical n”);

fclose ( fp1 );
fclose ( fp2 );
}
return(0);
}

Explanation :

Firstly Declare two file pointers for two files.
Open two files in read mode.
Now Inside while loop read both files character by character.
Check both characters whether they are equal or not.
If inside if statement ch1 = EOF and ch2=EOF then both files are said to be equal otherwise both files are non identicle.

C program to Copy the contents of one file into another using fputc

#include
#include
#include

void main()
{
FILE *fp1,*fp2;
char ch;
clrscr();

fp1 = fopen(“Sample.txt”,”r”);
fp2 = fopen(“Output.txt”,”w”);

while(1)
{
ch = fgetc(fp1);

if(ch==EOF)
break;
else
putc(ch,fp2);
}

printf(“File copied succesfully!”);
fclose(fp1);
fclose(fp2);
}
Step 1 : Open Source File in Read Mode
fp1 = fopen(“Sample.txt”,”r”);
Step 2 : Open Target File in Write Mode
fp2 = fopen(“Output.txt”,”w”);
Step 3 : Read Source File Character by Character
while(1)
{
ch = fgetc(fp1);

if(ch==EOF)
break;
else
putc(ch,fp2);
}

Connect to The MySQL Database

You should establish a connection to the MySQL database. This is an extremely important step because if your script cannot connect to its database, your queries to the database will fail.

A good practice when using databases is to set the username, the password and the database name values at the beginning of the script code. If you need to change them later, it will be an easy task.

$username=”your_username”;$password=”your_password”;$database=”your_database”;

You should replace “your_username”, “your_password” and “your_database” with the MySQL username, password and database that will be used by your script.

At this point you may be wondering if it is a security risk to keep your password in the file. You don’t need to worry because the PHP source code is processed by the server before being sent to the browser. So the visitor will not see the script’s code in the page source.

Next you should connect your PHP script to the database. This can be done with the mysql_connect PHP function:

mysql_connect(localhost,$username,$password);

This line tells PHP to connect to the MySQL database server at ‘localhost’ (localhost is the MySQL server which usually runs on the same physical server as your script).

After the connection is established you should select the database you wish to use. This should be a database to which your username has access to. This can be completed through the following command:

@mysql_select_db($database) or die( “Unable to select database”);

It tells PHP to select the database stored in the variable $database (in our case it will select the database “your_database”). If the script cannot connect it will stop executing and will show the error message:

Unable to select database

The ‘or die’ part is useful as it provides debugging functionality. However, it is not essential.

Another important PHP function is:

mysql_close();

This is a very important function as it closes the connection to the database server. Your script will still run if you do not include this function. And too many open MySQL connections can cause problems for your account. This it is a good practice to close the MySQL connection once all the queries are executed.

You have connected to the server and selected the database you want to work with. You can start querying the database now.

Php Mysql connecivity

Your website can have several PHP pages that all access your MySQL database. One time saving idea is to store your connection code in a separate file, and then include the file where needed.

For example, we can use the SQL code below to login to our MySQL database. Let’s save this code in a file called datalogin.php.
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";

//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL
“;

//select a database to work with
$selected = mysql_select_db(“examples”,$dbhandle)
or die(“Could not select examples”);

//execute the SQL query and return records
$result = mysql_query(“SELECT id, model,year FROM cars”);

//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
echo “ID:”.$row{‘id’}.” Name:”.$row{‘model’}.”Year: “. //display the results
$row{‘year’}.”
“;
}
//close the connection
mysql_close($dbhandle);
?>