Wednesday, September 19, 2012

Code#106:c program to restrict mouse pointer in a circle

| |
0 comments
Basic C Language Program to restrict mouse pointer in a circle is explained below with a detailed explanation,you will find it easy to go through this program, Sample input and output screen shots has been provide for the better  understandability to the user, leave a comment if you are satisfied with the code
Click Read more for the code

Read More

Code#105:c program to restrict mouse pointer in a rectangle

| |
0 comments
Basic C Language Program to restrict mouse in a rectangle is explained below with a detailed explanation,you will find it easy to go through this program, Sample input and output screen shots has been provide for the better understandability to the user, leave a comment if you are satisfied with the code
                                Click Read more for the code

Read More

Code#104:c program to determine which mouse button is clicked

| |
0 comments
Basic C Language Program to determine which mouse button is clicked   is explained below with a detailed explanation,you will find it easy to go through this program, Sample input and output screen shots has been provide for the better understandability to the user, leave a comment if you are satisfied with the code
                                                      Click Read more for the code

Read More

Code#103:C Program to get current position of mouse pointer

| |
0 comments
Basic C Language Program  to get current position of mouse pointer  is explained below with a detailed explanation,you will find it easy to go through this program, Sample input and output screen shots has been provide for the better understandability to the user, leave a comment if you are satisfied with the code
            Click Read more for the code

Read More

Tuesday, September 18, 2012

Code#102:C Program to hide mouse pointer

| |
0 comments

Basic C Language Program  to hide mouse pointer  is explained below with a detailed explanation,you will find it easy to go through this program, Sample input and output screen shots has been provide for the better understandability to the user, leave a comment if you are satisfied with the code
                                       Click Read more for the code

Read More

Code#101:C Program to display mouse pointer in graphics mode

| |
1 comments
#include<graphics.h>
#include<conio.h>
#include<dos.h>

int initmouse();
void showmouseptr();

union REGS i, o;

main()
{
   int status, gd = DETECT, gm;

   initgraph(&gd,&gm,"C:\\TC\\BGI");

   status = initmouse();

   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      showmouseptr();

   getch();
   return 0;
}

int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}

void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}
Read More

Code#100:C Program to display mouse pointer in textmode

| |
0 comments
#include<dos.h>
#include<conio.h>

int initmouse();
void showmouseptr();

union REGS i, o;

main()
{
   int status;

   status = initmouse();

   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      showmouseptr();

   getch();
   return 0;
}

int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}

void showmouseptr()
{
   i.x.ax = 1;
   int86(0X33,&i,&o);
}
Read More

Code#99:C Program to check if mouse support is available or not

| |
0 comments
#include<dos.h>
#include<conio.h>

int initmouse();

union REGS i, o;

main()
{
   int status;

   status = initmouse();

   if ( status == 0 )
      printf("Mouse support not available.\n");
   else
      printf("Mouse support available.\n");

   getch();
   return 0;
}

int initmouse()
{
   i.x.ax = 0;
   int86(0X33,&i,&o);
   return ( o.x.ax );
}
output:
Mouse support available
Read More

Code#98:C program to shutdown or turn off computer for Windows 7

| |
0 comments
#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown /s");

   return 0;
}
Read More

Code#97:C program to shutdown or turn off computer for Windows XP

| |
0 comments
#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now (y/n)\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");

   return 0;
}
Read More

Code#96:c program to print date

| |
0 comments
#include<stdio.h>
#include<conio.h>
#include<dos.h>

main()
{
   struct date d;

   getdate(&d);

   printf("Current system date is %d/%d/%d",d.da_day,d.da_mon,d.da_year);
   getch();
   return 0;
}
Output:
Current system date is 23/09/2012
Read More

Code #95:c program to add two complex numbers

| |
0 comments
#include <stdio.h>

struct complex
{
   int real, img;
};

main()
{
   struct complex a, b, c;

   printf("Enter a and b where a + ib is the first complex number.\n");
   printf("a = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.\n");
   printf("c = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);

   c.real = a.real + b.real;
   c.img = a.img + b.img;

   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %di\n",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di\n",c.real,c.img);

   return 0;
}
Enter a and b where a + ib is the first complex number
a = 4
b = 5
Enter c and c where c + id is the second complex number
c = 2
d = 6
Sum of two complex numbers =6 + 11i
Read More

Code#94:C programming code using random function

| |
0 comments
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

main()
{
   int n, max, num, c;

   printf("Enter the number of random numbers you want ");
   scanf("%d",&n);

   printf("Enter the maximum value of random number ");
   scanf("%d",&max);

   printf("%d random numbers from 0 to %d are :-\n",n,max);
   randomize();

   for ( c = 1 ; c <= n ; c++ )
   {
      num = random(max);
      printf("%d\n",num);

   }

   getch();
   return 0;
}
Read More

Code#93:c program to delete a file

| |
0 comments
#include<stdio.h>

main()
{
   int status;
   char file_name[25];

   printf("Enter the name of file you wish to delete\n");
   gets(file_name);

   status = remove(file_name);

   if( status == 0 )
      printf("%s file deleted successfully.\n",file_name);
   else
   {
      printf("Unable to delete the file\n");
      perror("Error");
   }

   return 0;
}
Output:
Enter the name of file you wish to delete add.c
add.c file deleted succesfully
Read More

Code#92:c program to list files in directory

| |
0 comments
#include<stdio.h>
#include<conio.h>
#include<dir.h>

main()
{
   int done;
   struct ffblk a;

   printf("Press any key to view the files in the current directory\n");

   getch();

   done = findfirst("*.*",&a,0);

   while(!done)
   {
      printf("%s\n",a.ff_name);
      done = findnext(&a);
   }

   getch();
   return 0;
}
Output:
press any key to view the files in the current directory
LISTFILE.C
LISTFILE.EXE
Read More

Code#91:c program to merge two files

| |
0 comments
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

main()
{
   FILE *fs1, *fs2, *ft;

   char ch, file1[20], file2[20], file3[20];

   printf("Enter name of first file ");
   gets(file1);

   printf("Enter name of second file ");
   gets(file2);

   printf("Enter name of file which will store contents of two files ");
   gets(file3);

   fs1 = fopen(file1,"r");
   fs2 = fopen(file2,"r");

   if( fs1 == NULL || fs2 == NULL )
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      getch();
      exit(EXIT_FAILURE);
   }

   ft = fopen(file3,"w");

   if( ft == NULL )
   {
      perror("Error ");
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   while( ( ch = fgetc(fs1) ) != EOF )
      fputc(ch,ft);

   while( ( ch = fgetc(fs2) ) != EOF )
      fputc(ch,ft);

   printf("Two files were merged into %s file successfully.\n",file3);

   fclose(fs1);
   fclose(fs2);
   fclose(ft);

   getch();
   return 0;
}
output:
Enter name of first file concat.c
Enter name of second file compare.c
Enter name of file which will store contents of two files combine.c
Two files were merged into combine.c file succesfully
Read More

Code#90:c program to copy files

| |
0 comments
#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch, source_file[20], target_file[20];
   FILE *source, *target;

   printf("Enter name of file to copy\n");
   gets(source_file);

   source = fopen(source_file, "r");

   if( source == NULL )
   {
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   printf("Enter name of target file\n");
   gets(target_file);

   target = fopen(target_file, "w");

   if( target == NULL )
   {
      fclose(source);
      printf("Press any key to exit...\n");
      exit(EXIT_FAILURE);
   }

   while( ( ch = fgetc(source) ) != EOF )
      fputc(ch, target);

   printf("File copied successfully.\n");

   fclose(source);
   fclose(target);

   return 0;
}
Output:
Enter name of file to copy concat.c
Enter name of  target file mycopy.c
File copied succesfully
Read More

Code#89:c program to read a file

| |
0 comments
C program to read a file :- This program reads a file entered by the user and displays it's contents on the screen, fopen function is used to open a file, it returns a pointer to structure FILE. FILE is a predefined structure in stdio.h . If the file is successfully opened then fopen returns a pointer to file and if it is unable to open a file then it returns NULL. fgetc function returns a character which is read from the file and fclose function closes the file. Opening a file means we bring file from disk to ram to perform operations on it. The file must be present in the directory in which the executable file of this code sis present.

#include<stdio.h>
#include<stdlib.h>

main()
{
   char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see ");
   gets(file_name);

   fp = fopen(file_name,"r"); // read mode

   if( fp == NULL )
   {
      perror("Error while opening the file.\n");
      exit(EXIT_FAILURE);
   }

   printf("The contents of %s file are :- \n\n", file_name);

    while( ( ch = fgetc(fp) ) != EOF )
      printf("%c",ch);

   fclose(fp);
   return 0;
}

Output:

read file  


 

Read More

Code#88:C Program to check for Anagrams

| |
0 comments
Anagram in c: c program to check whether two strings are anagrams or not, string is assumed to consist of alphabets only. Two words are said to be anagrams of each other if the letters from one word can be rearranged to form the other word. From the above definition it is clear that two strings are anagrams if all characters in both strings occur same number of times. For example "abc" and "cab" are anagram strings, here every character 'a', 'b' and 'c' occur only one time in both strings. Our algorithm tries to find how many times characters appears in the strings and then comparing their corresponding counts.
#include <stdio.h>

int check_anagram(char [], char []);

int main()
{
   char a[100], b[100];
   int flag;

   printf("Enter first string\n");
   gets(a);

   printf("Enter second string\n");
   gets(b);

   flag = check_anagram(a, b);

   if (flag == 1)
      printf("\"%s\" and \"%s\" are anagrams.\n", a, b);
   else
      printf("\"%s\" and \"%s\" are not anagrams.\n", a, b);

   return 0;
}

int check_anagram(char a[], char b[])
{
   int first[26] = {0}, second[26] = {0}, c = 0;

   while (a[c] != '\0')
   {
      first[a[c]-'a']++;
      c++;
   }

   c = 0;

   while (b[c] != '\0')
   {
      second[b[c]-'a']++;
      c++;
   }

   for (c = 0; c < 26; c++)
   {
      if (first[c] != second[c])
         return 0;
   }

   return 1;
}
Output:
Enter first string
care
Enter second string
race
"care" and "race" are anagrams
Read More

Code#87:c program to find frequency of characters in a string

| |
1 comments
#include<stdio.h>
#include<string.h>

main()
{
   char string[100], ch;
   int c = 0, count[26] = {0};

   printf("Enter a string\n");
   gets(string);

   while ( string[c] != '\0' )
   {
      /* Considering characters from 'a' to 'z' only */

      if ( string[c] >= 'a' && string[c] <= 'z' )
         count[string[c]-'a']++;

      c++;
   }

   for ( c = 0 ; c < 26 ; c++ )
   {
      if( count[c] != 0 )
         printf("%c occurs %d times in the entered string.\n",c+'a',count[c]);
   }

   return 0;
}


characters frequency in string
Read More

Code# 86:c program to swap two strings

| |
0 comments
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#include<conio.h>

main()
{
   char first[100], second[100], *temp;

   printf("Enter the first string ");
   gets(first);

   printf("Enter the second string ");
   gets(second);

   printf("\nBefore Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n\n",second);

   temp = (char*)malloc(100);

   strcpy(temp,first);
   strcpy(first,second);
   strcpy(second,temp);

   printf("After Swapping\n");
   printf("First string: %s\n",first);
   printf("Second string: %s\n",second);

   getch();
   return 0;
}

Output:
Enter the first string i have no girlfriend
Enter the second string i have no boyfriend

Before Swapping
First string: i have no girlfriend
Second string: i have no boyfriend

After Swapping
First string: i have no boyfriend
Second string: i have no girlfriend
Read More

Code#85:C Program Change string to lower case without strlwr

| |
1 comments
#include<stdio.h>

void lower_string(char*);

main()
{
   char string[100];

   printf("Enter a string to convert it into lower case\n");
   gets(string);

   lower_string(string);

   printf("Entered string in lower case is \"%s\"\n", string);

   return 0;
}

void lower_string(char *string)
{
   while(*string)
   {
      if ( *string >= 'A' && *string <= 'Z' )
      {
         *string = *string + 32;
      }
      string++;
   }
}
Read More

Code#84:C Program To Change string to upper case without strupr

| |
0 comments
#include<stdio.h>

void upper_string(char*);

main()
{
   char string[100];

   printf("Enter a string to convert it into upper case\n");
   gets(string);

   upper_string(string);

   printf("Entered string in upper case is \"%s\"\n", string);

   return 0;
}

void upper_string(char *string)
{
   while(*string)
   {
       if ( *string >= 'a' && *string <= 'z' )
       {
          *string = *string - 32;
       }
       string++;
   }
}
Read More

Code#83:c program remove spaces, blanks from a string using pointers

| |
0 comments
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SPACE ' '

main()
{
   char string[100], *blank, *start;
   int length, c = 0, d = 0;

   printf("Enter a string\n");
   gets(string);

   length = strlen(string);

   blank = string;

   start = (char*)malloc(length+1);

   if ( start == NULL )
      exit(EXIT_FAILURE);

   while(*(blank+c))
   {
      if ( *(blank+c) == SPACE && *(blank+c+1) == SPACE )
      {}
      else
      {
         *(start+d) = *(blank+c);
     d++;
      }
      c++;
   }
   *(start+d)='\0';

   printf("%s\n", start);

   free(start);    

   return 0;
}
Read More

Code# 82:c program to remove spaces, blanks from a string

| |
0 comments
#include <stdio.h>

int main()
{
   char text[100], blank[100];
   int c = 0, d = 0;

   printf("Enter some text\n");
   gets(text);

   while (text[c] != '\0')
   {
      if (!(text[c] == ' ' && text[c+1] == ' ')) {
        blank[d] = text[c];
        d++;
      }
      c++;
   }

   blank[d] = '\0';

   printf("Text after removing blanks\n%s\n", blank);

   return 0;
}
Read More

Code# 81:c program to sort a string in alphabetic order

| |
0 comments
Basic C Language Program  to sort a string in alphabetic order  is explained below with a detailed explanation,you will find it easy to go through this program, Sample input and output screen shots has been provide for the better understandability to the user, leave a comment if you are satisfied with the code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void sort_string(char*);

main()
{
   char string[100];

   printf("Enter some text\n");
   gets(string);

   sort_string(string);
   printf("%s\n", string);

   return 0;
}

void sort_string(char *s)
{
   int c, d = 0, length;
   char *pointer, *result, ch;

   length = strlen(s);

   result = (char*)malloc(length+1);

   pointer = s;

   for ( ch = 'a' ; ch <= 'z' ; ch++ )
   {
      for ( c = 0 ; c < length ; c++ )
      {
         if ( *pointer == ch )
         {
            *(result+d) = *pointer;
            d++;
         }
         pointer++;
      }
      pointer = s;
   }
   *(result+d) = '\0';

   strcpy(s, result);
   free(result);
}
Read More

Code# 80:C Program To Find All Substrings Of String

| |
0 comments
#include <stdio.h>
#include <string.h>
#include <malloc.h>

char* substring(char*, int, int);

main()
{
   char string[100], *pointer;
   int position = 1, length = 1, temp, string_length;

   printf("Enter a string\n");
   gets(string);

   temp = string_length = strlen(string);

   printf("Substring of \"%s\" are\n", string);

   while (position <= string_length)
   {
      while (length <= temp)
      {
         pointer = substring(string, position, length);
         printf("%s\n", pointer);
         free(pointer);
         length++;
      }
      temp--;
      position++;
      length = 1;
   }

   return 0;
}

/* Use substring function given in above c program*/ 

Output:
Enter a string
coding
Substring of "coding" are
c
co
cod
codi
codin
coding
o
od
odi
odin
oding
d
di
din
ding
i
in
ing
n
ng
g
Read More

Code#79:C Program To Find Substring

| |
0 comments
#include <stdio.h>
#include <malloc.h>

char* substring(char*, int, int);

main()
{
   char string[100], *pointer;
   int position, length;

   printf("Enter a string\n");
   gets(string);

   printf("Enter the position and length of substring\n");
   scanf("%d%d",&position, &length);

   pointer = substring( string, position, length);

   printf("Required substring is \"%s\"\n", pointer);

   free(pointer);

   return 0;
}

/*C substring function: It returns a pointer to the substring */

char *substring(char *string, int position, int length)
{
   char *pointer;
   int c;

   pointer = malloc(length+1);

   if (pointer == NULL)
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }

   for (c = 0 ; c < position -1 ; c++)
      string++;

   for (c = 0 ; c < length ; c++)
   {
      *(pointer+c) = *string;     
      string++;  
   }

   *(pointer+c) = '\0';

   return pointer;
}

Output:
Enter a string
I am feeling good
Enter the position and length of substring
6
12
Required substring is "feeling good"
Read More

Code#78:C Program TO Delete Vowel From Given String Using Pointers

| |
0 comments
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define TRUE 1
#define FALSE 0
 
int check_vowel(char);
 
main()
{
   char string[100], *temp, *pointer, ch, *start;
 
   printf("Enter a string\n");
   gets(string);
 
   temp = string;
   pointer = (char*)malloc(100);
 
  if( pointer == NULL )
   {
      printf("Unable to allocate memory.\n");
      exit(EXIT_FAILURE);
   }
 
   start = pointer;
 
   while(*temp)
   {
      ch = *temp;
 
      if ( !check_vowel(ch) )
      {
         *pointer = ch;
         pointer++;
      }
      temp++;
   }
   *pointer = '\0';
 
   pointer = start;
   strcpy(string, pointer); /* If you wish to convert original string */
   free(pointer);
 
   printf("String after removing vowel is \"%s\"\n", string);
 
   return 0;
}
 
int check_vowel(char a)
{
   if ( a >= 'A' && a <= 'Z' )
      a = a + 'a' - 'A';
 
   if ( a == 'a' || a == 'e' || a == 'i' || a == 'o' || a == 'u')
      return TRUE;
 
   return FALSE;
}
Read More

Code#77:C Program To delete Vowels from Given String

| |
0 comments
#include <stdio.h>
#include <string.h>

int check_vowel(char);

int main()
{
  char s[100], t[100];
  int i, j = 0;

  printf("Enter a string to delete vowels\n");
  gets(s);

  for(i = 0; s[i] != '\0'; i++) {
    if(check_vowel(s[i]) == 0) {       //not a vowel
      t[j] = s[i];
      j++;
    }
  }

  t[j] = '\0';

  strcpy(s, t);    //We are changing initial string

  printf("String after deleting vowels: %s\n", s);

  return 0;
}


int check_vowel(char c)
{
  switch(c) {
    case 'a':
    case 'A':
    case 'e':
    case 'E':
    case 'i':
    case 'I':
    case 'o':
    case 'O':
    case 'u':
    case 'U':
      return 1;
    default:
      return 0;
  }
}
Read More

Monday, September 17, 2012

Code#76:C program to check string is palindrome or not without using string functions

| |
0 comments

#include <stdio.h>
#include <string.h>

main()
{
   char text[100];
   int begin, middle, end, length = 0;

   gets(text);

   while ( text[length] != '\0' )
      length++;

   end = length - 1;
   middle = length/2;

   for( begin = 0 ; begin < middle ; begin++ )
   {
      if ( text[begin] != text[end] )
      {
         printf("Not a palindrome.\n");
         break;
      }
      end--;
   }
   if( begin == middle )
      printf("Palindrome.\n");

   return 0;
}
Read More

Code#75:C program to check string is palindrome or not

| |
0 comments

#include <stdio.h>
#include <string.h>

main()
{
   char a[100], b[100];

   printf("Enter the string to check if it is a palindrome\n");
   gets(a);

   strcpy(b,a);
   strrev(b);

   if( strcmp(a,b) == 0 )
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");

   return 0;
}
Read More

Code# 74:C program to reverse a string using recursion

| |
0 comments

#include<stdio.h>
#include<string.h>

void reverse(char*,int,int);

main()
{
   char a[100];

   gets(a);

   reverse(a, 0, strlen(a)-1);

   printf("%s\n",a);

   return 0;
}

void reverse(char *x, int beg, int end)
{
   char a, b, c;

   if ( beg >= end )
      return;   

   c = *(x+beg);
   *(x+beg) = *(x+end);
   *(x+end) = c;

   reverse(x, ++beg, --end);
}
Read More

Code#73:C program to reverse a string using pointers

| |
0 comments

#include<stdio.h>

int string_length(char*);
void reverse(char*);

main() 
{
   char string[100];

   printf("Enter a string\n");
   gets(string);

   reverse(string);

   printf("Reverse of entered string is \"%s\".\n", string);

   return 0;
}

void reverse(char *string) 
{
   int length, c;
   char *begin, *end, temp;

   length = string_length(string);

   begin = string;
   end = string;

   for ( c = 0 ; c < ( length - 1 ) ; c++ )
      end++;

   for ( c = 0 ; c < length/2 ; c++ ) 
   {        
      temp = *end;
      *end = *begin;
      *begin = temp;

      begin++;
      end--;
   }
}

int string_length(char *pointer)
{
   int c = 0;

   while( *(pointer+c) != '\0' )
      c++;

   return c;
}
Read More

Code#72:C Program To Reverse a String

| |
0 comments
Basic C Language Program  To Reverse a String is explained below with a detailed explanation,you will find it easy to go through this program, Sample input and output screen shots has been provide for the better understand ability to the user, leave a comment if you are satisfied with the code
                                            Click Read more to see the code

Read More

Code#71:c program to concatenate strings Without Using strcat()

| |
0 comments

#include<stdio.h>

void concatenate_string(char*, char*);

main()
{
    char original[100], add[100];

    printf("Enter source string\n");
    gets(original);

    printf("Enter string to concatenate\n");
    gets(add);

    concatenate_string(original, add);

    printf("String after concatenation is \"%s\"\n", original);

    return 0;
}

void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;

   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}
Read More

Code#70:c program to concatenate strings

| |
0 comments

#include<stdio.h>
#include<conio.h>
#include<string.h>

main()
{
   char a[100], b[100];

   printf("Enter the first string\n");
   gets(a);

   printf("Enter the second string\n");
   gets(b);

   strcat(a,b);

   printf("String obtained on concatenation is %s\n",a);

   getch();
   return 0;
}
Output:

Enter the first string
under
Enter the second string
stand
string obtained on concatenation is understand
Read More

Code#69:c program to copy a string using pointers

| |
0 comments

#include<stdio.h>

void copy_string(char*, char*);

main()
{
    char source[100], target[100];

    printf("Enter source string\n");
    gets(source);

    copy_string(target, source);

    printf("Target string is \"%s\"\n", target);

    return 0;
}

void copy_string(char *target, char *source)
{
   while(*source)
   {
      *target = *source;
      source++;
      target++;
   }
   *target = '\0';
}
Read More

Code#68:C program to copy a string

| |
0 comments

#include<stdio.h>
#include<string.h>

main()
{
   char source[] = "C program";
   char destination[50];

   strcpy(destination, source);

   printf("Source string: %s\n", source);
   printf("Destination string: %s\n", destination);

   return 0;
}
Read More

Code#67:C program to compare two strings using pointers

| |
0 comments

#include<stdio.h>

int compare_string(char*, char*);

main()
{
    char first[100], second[100], result;

    printf("Enter first string\n");
    gets(first);

    printf("Enter second string\n");
    gets(second);

    result = compare_string(first, second);

    if ( result == 0 )
       printf("Both strings are same.\n");
    else
       printf("Entered strings are not equal.\n");

    return 0;
}

int compare_string(char *first, char *second)
{
   while(*first==*second)
   {
      if ( *first == '\0' || *second == '\0' )
         break;

      first++;
      second++;
   }
   if( *first == '\0' && *second == '\0' )
      return 0;
   else
      return -1;
}
Read More

Code#66:C program to compare two strings using strcmp

| |
0 comments

#include<stdio.h>
#include<string.h>

main()
{
   char a[100], b[100];

   printf("Enter the first string\n");
   gets(a);

   printf("Enter the second string\n");
   gets(b);

   if( strcmp(a,b) == 0 )
      printf("Entered strings are equal.\n");
   else
      printf("Entered strings are not equal.\n");

   return 0;
}
Read More

Code# 65:C Program To Find Length of String Without Using strlen()

| |
0 comments

#include<stdio.h>

main()
{
   char array[100], *pointer;
   int length = 0;

   printf("Enter a string\n");
   gets(array);

   pointer = array;

   while(*(pointer+length))
      length++;

   printf("Length of entered string = %d\n",length);

   return 0;
}
Read More

Code# 64:C Program To Find Length of String

| |
0 comments

#include<stdio.h>
#include<string.h>

main()
{
   char a[100];
   int length;

   printf("Enter a string to calculate it's length\n");
   gets(a);

   length = strlen(a);

   printf("Length of entered string is = %d\n",length);

   return 0;
}
Output:
Enter a string to calculate it's length
c programming
Length of entered string is =13
Read More

Code#63:C Program To Print String

| |
0 comments

#include <stdio.h>

main()
{
    char array[100];

    printf("Enter a string\n");
    scanf("%s", array);

    printf("You entered the string %s\n",array);
    return 0;
}
Read More

Code#62:C Program To Find Transpose Of Matrix

| |
0 comments

This c program prints transpose of a matrix. It is obtained by interchanging rows and columns of a matrix. For example if a matrix is
1 2
3 4
5 6
then transpose of above matrix will be
1 3 5
2 4 6
When we transpose a matrix then the order of matrix changes, but for a square matrix order remains same.

#include<stdio.h>

main()
{
   int m, n, c, d, matrix[10][10], transpose[10][10];

   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d",&m,&n);
   printf("Enter the elements of matrix \n");

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         scanf("%d",&matrix[c][d]);
      }
   }

   for( c = 0 ; c < m ; c++ )
   {
      for( d = 0 ; d < n ; d++ )
      {
         transpose[d][c] = matrix[c][d];
      }
   }

   printf("Transpose of entered matrix :-\n");

   for( c = 0 ; c < n ; c++ )
   {
      for( d = 0 ; d < m ; d++ )
      {
         printf("%d\t",transpose[c][d]);
      }  
      printf("\n");
   }

   return 0;
}
Output:

Enter the number of rows and columns of matrix
4
2
Enter the elements of amtrix
1 2
3 4
5 6
7 8
Transpose of entered matrix:-
1 3 5 7
2 4 6 8


Read More

Code# 61:C Program To Multiply two Matrices

| |
0 comments

#include <stdio.h>

int main()
{
  int m, n, p, q, c, d, k, sum = 0;
  int first[10][10], second[10][10], multiply[10][10];

  printf("Enter the number of rows and columns of first matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (  c = 0 ; c < m ; c++ )
    for ( d = 0 ; d < n ; d++ )
      scanf("%d", &first[c][d]);

  printf("Enter the number of rows and columns of second matrix\n");
  scanf("%d%d", &p, &q);

  if ( n != p )
    printf("Matrices with entered orders can't be multiplied with each other.\n");
  else
  {
    printf("Enter the elements of second matrix\n");

    for ( c = 0 ; c < p ; c++ )
      for ( d = 0 ; d < q ; d++ )
        scanf("%d", &second[c][d]);

    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
      {
        for ( k = 0 ; k < p ; k++ )
        {
          sum = sum + first[c][k]*second[k][d];
        }

        multiply[c][d] = sum;
        sum = 0;
      }
    }

    printf("Product of entered matrices:-\n");

    for ( c = 0 ; c < m ; c++ )
    {
      for ( d = 0 ; d < q ; d++ )
        printf("%d\t", multiply[c][d]);

      printf("\n");
    }
  }

  return 0;
}
Output:

Enter the number of rows and columns of first matrix
3 3
Enter the elements of first matrix
0 -1 -1
6 2 4
-4 3 7
Enter the rows and columns of second matrix
3 3
Enter the elements of second matrix
-3 5 8
-1 1 0
-4 3 2
Product of entered matrices:-
-3 2 2
-36 44 56
-19 4 -18

Read More

Code# 60:C Program To Subtract two Matrices

| |
0 comments

#include<stdio.h>

int main()
{
  int m, n, c, d, first[10][10], second[10][10], difference[10][10];

  printf("Enter the number of rows and columns of matrix\n");
  scanf("%d%d", &m, &n);
  printf("Enter the elements of first matrix\n");

  for (c = 0; c < m; c++)
    for (d = 0 ; d < n; d++)
      scanf("%d", &first[c][d]);

  printf("Enter the elements of second matrix\n");

  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
        scanf("%d", &second[c][d]);

  for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
      difference[c][d] = first[c][d] - second[c][d];

  printf("difference of entered matrices:-\n");

  for (c = 0; c < m; c++)
  {
    for (d = 0; d < n; d++)
      printf("%d\t",difference[c][d]);

    printf("\n");
  }

  return 0;
}
Read More

Code# 59:C Program To Add two Matrices

| |
0 comments

#include <stdio.h>

main()
{
   int m, n, c, d, first[10][10], second[10][10], sum[10][10];

   printf("Enter the number of rows and columns of matrix ");
   scanf("%d%d", &m, &n);
   printf("Enter the elements of first matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         scanf("%d", &first[c][d]);

   printf("Enter the elements of second matrix\n");

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
            scanf("%d", &second[c][d]);

   for ( c = 0 ; c < m ; c++ )
      for ( d = 0 ; d < n ; d++ )
         sum[c][d] = first[c][d] + second[c][d];

   printf("Sum of entered matrices:-\n");

   for ( c = 0 ; c < m ; c++ )
   {
      for ( d = 0 ; d < n ; d++ )
         printf("%d\t", sum[c][d]);

      printf("\n");
   }

   return 0;
}
Output:

Enter the number of rows and columns of matrix 2 3
Enter the elements of first matrix
1 2 3 
4 5 6
Enter the elements of second matrix
7 8 9 
1 2 3
Sum of entered matrices:-
8 10 12
5 7  9

Read More

Code#58:C Program To Implement Selection Sort

| |
0 comments

#include<stdio.h>

main()
{
   int array[100], n, c, d, position, swap;

   printf("Enter number of elements\n");
   scanf("%d", &n);

   printf("Enter %d integers\n", n);

   for ( c = 0 ; c < n ; c++ )
      scanf("%d", &array[c]);

   for ( c = 0 ; c < ( n - 1 ) ; c++ )
   {
      position = c;

      for ( d = c + 1 ; d < n ; d++ )
      {
         if ( array[position] > array[d] )
            position = d;
      }
      if ( position != c )
      {
         swap = array[c];
         array[c] = array[position];
         array[position] = swap;
      }
   }

   printf("Sorted list in ascending order:\n");

   for ( c = 0 ; c < n ; c++ )
      printf("%d\n", array[c]);

   return 0;
}
Read More

Code#57:C Program To Perform Insertion Sort

| |
0 comments

/* insertion sort ascending order */

#include <stdio.h>

int main()
{
  int n, array[1000], c, d, t;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++) {
    scanf("%d", &array[c]);
  }

  for (c = 1 ; c <= n - 1; c++) {
    d = c;

    while ( d > 0 && array[d] < array[d-1]) {
      t          = array[d];
      array[d]   = array[d-1];
      array[d-1] = t;

      d--;
    }
  }

  printf("Sorted list in ascending order:\n");

  for (c = 0; c <= n - 1; c++) {
    printf("%d\n", array[c]);
  }

  return 0;
}
Output:

Enter number of elements
5
Enter 5 integers
4
1
2
5
7
Sorted list in ascending order
1
2
4
5
7

Read More

Code# 56:C Program for Bubble sort

| |
0 comments

/* Bubble sort code */

#include <stdio.h>

int main()
{
  int array[100], n, c, d, swap;

  printf("Enter number of elements\n");
  scanf("%d", &n);

  printf("Enter %d integers\n", n);

  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);

  for (c = 0 ; c < ( n - 1 ); c++)
  {
    for (d = 0 ; d < n - c - 1; d++)
    {
      if (array[d] > array[d+1]) /* For decreasing order use < */
      {
        swap       = array[d];
        array[d]   = array[d+1];
        array[d+1] = swap;
      }
    }
  }

  printf("Sorted list in ascending order:\n");

  for ( c = 0 ; c < n ; c++ )
     printf("%d\n", array[c]);

  return 0;
}
Read More

Code# 55:C Program To Merge Two Arrays

| |
0 comments

#include <stdio.h>

void merge(int [], int, int [], int, int []);

int main() {
  int a[100], b[100], m, n, c, sorted[200];

  printf("Input number of elements in first array\n");
  scanf("%d", &m);

  printf("Input %d integers\n", m);
  for (c = 0; c < m; c++) {
    scanf("%d", &a[c]);
  }

  printf("Input number of elements in second array\n");
  scanf("%d", &n);

  printf("Input %d integers\n", n);
  for (c = 0; c < n; c++) {
    scanf("%d", &b[c]);
  }

  merge(a, m, b, n, sorted);

  printf("Sorted array:\n");

  for (c = 0; c < m + n; c++) {
    printf("%d\n", sorted[c]);
  }

  return 0;
}

void merge(int a[], int m, int b[], int n, int sorted[]) {
  int i, j, k;

  j = k = 0;

  for (i = 0; i < m + n;) {
    if (j < m && k < n) {
      if (a[j] < b[k]) {
        sorted[i] = a[j];
        j++;
      }
      else {
        sorted[i] = b[k];
        k++;
      }
      i++;
    }
    else if (j == m) {
      for (; i < m + n;) {
        sorted[i] = b[k];
        k++;
        i++;
      }
    }
    else {
      for (; i < m + n;) {
        sorted[i] = a[j];
        j++;
        i++;
      }
    }
  }
}
Output:
Input number of elements in first array
2
input 2 integers
4
5
Input number of elements in second array
4
Input 4 integers
3
1
3
4
Sorted array:
1
3
3
4
4
5


Read More
Powered by Blogger.