Friday, January 30, 2009

Write a C program to displays the position or index in the string S where the string T begins, or -1 if S doesn't contain T.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
void main( )
{
char st[10], t[10];
int i, flag=0, count=0, j, pos;
clrscr();
printf("Enter the main string:");
gets(st);
printf("Enter the sub string:");
gets(t);
for(i=0; st[i]!='\0'; i++)
{
if(st[i]==t[0])
{
pos=i;
for(j=0; t[j]!='\0'; j++)
{
if(st[i]==t[j])
count++;
i++;
}
flag=1;
break;
}
}
if(flag==1 && count==strlen(t))
printf("\nPosition: %d, And T is a substring of S", pos+1);
else if(flag==1 && count < strlen(t))
printf("\nPosition: %d, And T is not a substring of S", pos+1);
else
printf("\nIndex: -1");
getch();
}

Monday, January 26, 2009

Write a C program to Shut down the computer.

int main( )
{
system("shutdown -s");
return 0;
}

Explanation:-
Save the above .Let file name is shut.c and compile and execute the above program. Now close the turbo c compiler and open the directory in window where you have saved the internet.c ( default directory c:\tc\bin)and double click the its exe file( shut.exe).

Write a C program to count the lines, words and characters in a given text.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
#include(ctype.h)
int main( )
{
char ch;
int line=0, space=0, ct=0;
clrscr( );
printf("Enter the required no. of lines:\n");
while((ch=getchar( ))!=EOF)
{
if(ch==10) { line++; }
if(isspace(ch)) { space++; }
ct++;
}
printf("\nNumber of Lines : %d", line+1);
printf("\nNumber of Words: %d", space+1);
printf("\nTotal Number of Characters: %d", ct);
getch( );
return 0;
}
Explanation:-
Here the 1st condition (ch==10) checking whether the line is ended or not. And ascii value of enter key is 10. (Note:- After completing every line, press enter key. Then only it counts as Line).
The 2nd condition (isspace(ch) checking for spaces between the words. And here, isspace is a special function and it checks whether the character is space or not.
Note:- After completing all the lines, press Ctrl + Z to stop the loop.

Monday, January 19, 2009

Write a C program which copies one file to another.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main( )
{
FILE *fp, *fq ;
char file1[20], file2[20];
char ch;
clrscr( );
printf("Enter the source filename to be copied:");
gets(file1);
fp=fopen(file1, "r");
if(fp==NULL)
{
printf("\nCannot open %s",file1);
exit(0);
}
printf("\nEnter the destination filename:");
gets(file2);
fq=fopen(file2, "w");
while((ch=getc(fp))!=EOF )
{ putc(ch,fq); }
printf("\n Copied Sucessfully..");
fclose(fp);
fclose(fq);
getch( );
return 0;
}

Note:- If u have any doubt regarding this program or logic, please feel free to contact me.

Thursday, January 8, 2009

Write a C program to generate Pascal�s triangle.

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
int arr[10][10];
int i, j, k;
clrscr();
printf("\nProgram for printing Pascal's Triangle:\n\n\n");
for(i=0; i<10; i++)
{
j=1;
arr[i][0]=1;
arr[i][i]=1;
while( j< i) {
arr[i][j]=arr[i-1][ j-1]+arr[i-1][ j];
j++; }
}
for(i=0;i <10;i++) //display part
{
j=10;
while( j>i) { printf(" "); //mention 2 spaces betw the double quotes
j--;
}
for(k=0;k<=i;k++)

{
printf("%4d",arr[i][k]);
}
printf("\n\n");
} //display for loop close
getch();
return 0;
} //main close

Write a C program to determine if the given string is a Palindrome or not without using library functions

/* Palindrome string means
ie., string = after reverse
given string= reverse string (then it is said to be palindrome string). */

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
char st[10], rev[10];
int i, len, flag;
clrscr( );
printf("Enter the required string: ");
scanf(" %s", st); // (or) gets(st);
for(i=0; st[i ]!='\0' ; i++ ); //to find the length of a given string
len=i -1;
for(i=0; st[i ]!='\0' ; i++) // to store the given string in reverse
{
rev[i ]=st[ len];
len--;
} rev[i]='\0';
flag=0;
for(i=0; st[i ]!='\0'; i++) // to compare the given and reverse strings.
{
if( st[i] !=rev[i ] ) { flag=1; break; }
}
if(flag==0)
printf("\nPalindrome string");
else
printf("\nNot a palindrome string");
getch( );
return 0;
}

Explanation:-
Here 'flag' value will tells that whether the given string is palindrome string or not.
ie., if(st[i] !=rev[i ]) { flag=1; break; }
The above statement is true, when the value of 'st' variable is not equal to 'rev' variable. So if the condition is true, then the control enter inside the block. First 'flag' value becomes One and the loop will terminate.
Therefore, the result palindrome will be displayed when 'flag value is zero'.

Wednesday, January 7, 2009

Program to find the distance travelled by Vehicle.

/* The total distance travelled by vehicle in 't' seconds is given by distance = ut+1/2at^2 where 'u' and 'a' are the initial velocity (m/sec.) and acceleration (m/sec2).
Write C program to find the distance travelled at regular intervals of time given the values of 'u' and 'a'. The program should provide the flexibility to the user to select his own time intervals and repeat the calculations for different values of 'u' and 'a'. */

#include(stdio.h) // place this '<' & '>' instead of '(' & ')' before stdio.h
#include(conio.h)
int main(void)
{
////////////////////This program will be developed later//////////////
getch( );
return 0;
}