Follow Us

LightBlog

Breaking

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

Wednesday, March 1, 2017

March 01, 2017

Write a c program for quick sort ?

Write a c program for quick sort ?

#include<stdio.h>
void quicksort(int [30],int,int);
int main()
{
  int array[40],size,i;

  printf("Enter size of the array: ");
  scanf("%d",&size);

  printf("Enter %d elements: ",size);
  for(i=0;i<size;i++)
    scanf("%d",&array[i]);
  quicksort(x,0,size-1);
  printf("Sorted elements: ");
  for(i=0;i<size;i++)
    printf(" %d",array[i]);
  return 0;
}
void quicksort(int array[30],int first,int last)
{
    int pivot,j,temp,i;
     if(first<last)
{
         pivot=first;
         i=first;
         j=last;
         while(i<j)
{
             while(array[i]<=array[pivot]&&i<last)
                 i++;
             while(array[j]>array[pivot])
                 j--;
             if(i<j)
{
                 temp=array[i];
                  array[i]=array[j];
                  array[j]=temp;
             }
         }

         temp=array[pivot];
         array[pivot]=array[j];
         array[j]=temp;
         quicksort(array,first,j-1);
         quicksort(array,j+1,last);

    }
}

Output:
Enter size of the array: 5
Enter 5 elements: 3 8 0 1 2
Sorted elements: 0 1 2 3 8
March 01, 2017

Write a c program for selection sort ?

Write a c program for selection sort ?
#include<stdio.h>
int main()
{

  int n,i,j,temp,array[20];

  printf("Enter total elements: ");
  scanf("%d",&n);

  printf("Enter %d elements: ",n);
  for(i=0;i<n;i++)
      scanf("%d",&array[i]);

  for(i=0;i<n;i++)
{
      for(j=i+1;j<n;j++)
{
           if(array[i]>array[j])
{
               temp=array[i];
              array[i]=array[j];
              array[j]=temp;
           }
      }
  }

  printf("After sorting is: ");
  for(i=0;i<n;i++)
      printf(" %d",array[i]);

  return 0;
}

Output:
Enter total elements: 5
Enter 5 elements: 4 5 0 21 7
The array after sorting is:  0 4 5 7 21
March 01, 2017

Write a c program for insertion sort ?

Write a c program for insertion sort ?

#include <stdio.h>
int main()
{
  int n, a[100], c, d, temp;

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

  for (c = 1 ; c <= n - 1; c++) 
{
    d = c;
    while ( d > 0 && a[d] < a[d-1]) 
{
      temp = a[d];
      a[d]   = a[d-1];
      a[d-1] = temp;

      d--;
    }
  }

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

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

  return 0;
}
March 01, 2017

Write a c program for bubble sort ?


Write a c program for bubble sort ?

#include<stdio.h>
int main()
 {
 int array[100], n, i, j, temp = 0;

 printf("Enter how many numbers you want:\n");
 scanf("%d", &n);

 printf("Enter the %d elements:\n", n);
 for (i = 0; i < n; i++) 
{
  scanf("%d", &array[i]);
 }

 printf("\nThe given array is:\n");
 for (i = 0; i < n; i++) 
{
  printf("\n%d", array[i]);
 }
 for (i = 0; i < n; i++)
 {
  for (j = i + 1; j < n; j++)
 {
   if (array[i] > array[j])
 {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
   }
  }
 }

 printf("\n\n\n\t\tThe sorted array using Buble sort is:\n");
 for (i = 0; i < n; i++)
 {
  printf("\n\t\t%d", array[i]);
 }
 return 0;
}
March 01, 2017

Write a c program to swap two numbers ?


Write a c program to swap two numbers ?


#include <stdio.h> 
#include <conio.h>
int main()
{
   int a, b, temp;
   printf("Enter the value of x and y\n");
   scanf("%d%d", &a, &b);

   printf("Before Swapping\nx = %d\ny = %d\n",a,b);

   temp = a;
   a   = b;
   b   = temp;

   printf("After Swapping\nx = %d\ny = %d\n",a,b);

   return 0;
}
March 01, 2017

Write a c program to swap two numbers without using third variable ?

Write a c program to swap two numbers without using third variable ?

#include<stdio.h>
#include<conio.h>
int main() 
{
   int x, y;
   printf("\nEnter value for num1 & num2 : ");
   scanf("%d %d", &x, &y);
   x = x + y;
   y = x - x;
   x = x - y;
   printf("\nAfter swapping value of a : %d", x);
   printf("\nAfter swapping value of b : %d", y);
   return (0);
}

Output

Enter value for num1 & num2 : 20 10
After swapping value of a : 10

After swapping value of b : 20
March 01, 2017

Write a c program for swapping of two arrays ?

       Write a c program for swapping of two arrays ?

    #include<stdio.h>
    int main()
 {
    int str1[100],str2[100],str3[100],i;
    printf("Enter First array->");
    for (i=0;i<100;i++)
     scanf("%d",&str1[i]);
    printf("\nEnter Second array->");
    for (i=0;i<100;i++)
               scanf("%d",&str2[i]);
    printf("Arrays before swapping");
    printf("\nFirst array->");
    for (i=0;i<100;i++) 
        {
    printf("%d",str1[i]);
    }
    printf("\nSecond array->");
    for (i=0;i<100;i++) 
        {
    printf("%d",str2[i]);
    }
    for (i=0;i<100;i++)
               {
   
    str3[i]=str1[i];
    str1[i]=str2[i];
    str2[i]=str3[i];
    }
    printf("\nArrays after swapping");
    printf("\nFirst array->");
    for (i=0;i<100;i++)
                {
    printf("%d",str1[i]);
    }
    printf("\nSecond array->");
    for (i=0;i<100;i++) 
                  {
    printf("%d",str2[i]);
    }
    return 0;
    }
March 01, 2017

Write a c program for swapping of two string ?



Write a c program for swapping of two string ?

#include <stdio.h>
#include <string.h>
#include <malloc.h>
int main()
{
   char f[100], str[100], *tmp;

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

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

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

   tmp = (char*)malloc(100);

   strcpy(tmp,f);
   strcpy(f,str);
   strcpy(str,t);

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

   return 0;
}


March 01, 2017

Convert a string to ASCII in c ?


Convert a string to ASCII in c ?

#include <stdio.h>
#include<conio.h>
void main()
{
  char string[200];
    int n, c = 0;

   printf("Enter the no of characters present in an array \n ");

    scanf("%d", &n);

    printf(" Enter the string of %d characters \n" , n);

    scanf("%s", string);

    while (c < n)

    {

        printf(" %c = %d\n", string[count], string[c] );

        ++ c ;

    }

}

Tuesday, February 28, 2017

February 28, 2017

String copy without using strcpy in c ?


String copy without using strcpy in c ?

#include <stdio.h>
#include <conio.h>
int main()
{
    char str1[100], str2[100], i;

    printf("Enter string str1: ");
    scanf("%str",str1);

    for(i = 0; str1[i] != '\0'; ++i)
    {
        str2[i] = str1[i];
    }

    s2[i] = '\0';
    printf("String str2: %s", s2);

    return 0;
}
February 28, 2017

How to compare two strings in c without using strcmp ?


 How to compare two strings in c without using strcmp ?

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char s1[5],s2[5];
int i,temp = 0;
printf("Enter the string1 value:\n");
gets(s1);
printf("\nEnter the String2 value:\n");
gets(s2);
for(i=0; (s1[i]!='\0')||(s2[i]!='\0'); i++)
{
if(s1[i] != s2[i])
{
temp = 1;
break;
}


}
if(temp == 0)
printf("Both strings are same.");
else
printf("Both strings not same.");
    return 0;
}
February 28, 2017

String concatenation in c without using strcat ?


String concatenation in c without using strcat ?


#include<stdio.h>
#include<conio.h>
void main(void)
{
  char s1[25],s2[25];
  int i=0,k=0;
  printf("\nEnter First String:");
  gets(s1);
  printf("\nEnter Second String:");
  gets(s2);
  while(s1[i]!='\0')
  i++;
  while(s2[k]!='\0')
  {
    s1[i]=s2[k];
    j++;
    k++;
  }
  s1[i]='\0';
  printf("\nConcatenated String is %s",s1);
}
February 28, 2017

Reverse a string using recursion in c ?



Reverse a string using recursion in c ?

# include <stdio.h>
#include<conio.h>
void reverse(char *string)
{
   if (*string)
   {
       reverse(string+1);
       printf("%c", *string);
   }
}
int main()
{
   char array[] = "Asmit for Rahul";
   reverse(array);
   return 0;
}

Output:

timsA rof luhaR
February 28, 2017

WAP to reverse a string ?


 WAP to reverse a string ?


#include<stdio.h>
#include<string.h>
int main()

 {
   char string[50], temp;
   int i, k= 0;

   printf("\nEnter the string :");
   gets(string);

   i = 0;
   k= strlen(string) - 1;
   while (i < k)
 {
      temp = string[i];
      string[i] = string[k];
      string[k] = temp;
      i++;
      k--;
   }

   printf("\nReverse string is :%s", string);
   return (0);
}

Out put
         
Enter the string  : Asmit
Reverse string is : timsA
February 28, 2017

WAP to print the string from given character ?



 WAP to print the string from given character ?

#include<string.h>
#include<stdio.h>
int main()
{
  char *ptr;
  char str[20],str1[1];
  printf("\nplease enter a string: ");
  scanf("%[^\n]",str);
  fflush(stdin);
  printf("\nplease enter character: ");
  gets(str1);
  ptr=strpbrk(str,str1);
  printf("\nThe string from the given character is: %s",ptr);
  return 0;
}

February 28, 2017

WAP which prints initial of any name ?

WAP which prints initial of any name ?

#include<stdio.h>
#include<conio.h>
int main()
{
    char string[200];
    int i=0;
    printf("Enter a string: ");
    gets(string);
    printf("%c", *string);
    while(string[i] != '\0')
    {
        if(string[i] == ' ')
        {
            i++;
            printf("%c", *(string + i));
        }
        i++;
    }
    return 0;
}

OUTPUT:-
                    Enter a string:
                   C Programming Simply


                    CPS

February 28, 2017

WAP to find the length of a string using pointer ?


WAP to find the length of a string using pointer ? 


#include<stdio.h>
#include<stdio.h>
#include<conio.h>
void main()
     {
    char str[100];

    char *p;

    int length;

    clrscr();

    printf("Enter a string\n");

    scanf("%s",str);

    for(p=str;*p!='\0';p++);

    length=p-str;

    printf("Length of string is::%d",length);

    getch();

     }
February 28, 2017

6. WAP for concatenation two strings without using string.h header file ?

 WAP for concatenation two strings without using string.h header file ?


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
   int n,i,c,flag,j;
   c=1;
   char string[100],a[100],ch;
   printf("enter a string");
   scanf("%s",string);
   printf("%s",string);
   n=strlen(string);
   printf("%d\n",n);
   a[0]=string[0];
   for(i=1;i<n;i++)
{
      flag=1;
      ch=string[i];
      for(j=0;j<=i;j++)
      {
          if(a[j]==ch)
          flag=0;
      }                        

   if(flag==1)
  {
    c++;
    a[i]=string[i];
  }

}
printf("%d\n",count);
system("pause");
}
February 28, 2017

WAP to sort the characters of a string.


 WAP to sort the characters of a string?


#include<stdio.h>
#include<conio.h>
int main()
{
  int i,k,n;
  char string[20][20],temp[20];
  puts("Enter the no. of string to be sorted");
  scanf("%d",&n);
  for(i=0;i<=n;i++)
      gets(string[i]);
  for(i=0;i<=n;i++)
      for(k=i+1;j<=n;k++)
{
           if(strcmp(string[i],string[k])>0)
{
               strcpy(temp,string[i]);
              strcpy(string[i],string[k]);
              strcpy(string[k],temp);
           }
      }
  printf("The sorted string\n");
  for(i=0;i<=n;i++)
      puts(string[i]);
  return 0;
}
February 28, 2017

WAP to count the different types of characters in given string?

WAP to count the different types of characters in given string?


#include <stdio.h>
int isvowel(char chk);
int main()
{
  char text[1000], chk;
  int count;
  count = 0;
  while((text[count] = getchar()) != '\n')
            count++;
  text[count] = '\0';
  count = 0;
  while ((chk = text[count]) != '\0')
{
      if (isvowel(chk))
{
           if((chk = text[++count]) && isvowel(chk))
{
               putchar(text[count -1]);
              putchar(text[count]);
              putchar('\n');
           }
      }
      else
           ++count;
  }
  return 0;
}
int isvowel(char chk)
{
  if(chk == 'a' || chk == 'e' || chk == 'i' || chk == 'o' || chk == 'u')
      return 1;
  return 0;
}