#include<stdio.h> main() { char str[150]; int i,j; printf("Enter a string: "); gets(str); for(i=0; str[i]!='\0'; i++) { while (!((str[i]>='a'&&str[i]<='z') || (str[i]>='A'&&str[i]<='Z' || str[i]=='\0'))) { for(j=i;str[j]!='\0';++j) { str[j]=str[j+1]; } str[j]='\0'; } } printf("Output String: "); puts(str); }
New Android apps UniqueKey
Sunday, 22 February 2015
Removing white space from a string.
Find no. of vowels , consonants , digits and white spaces in a string.
#include<stdio.h> main() { char str[150]; int i,v,c,ch,d,s,o; o=v=c=ch=d=s=0; printf("Enter a string:\n"); gets(str); for(i=0;str[i]!='\0';i++) { if(str[i]=='a' || str[i]=='e' || str[i]=='i' || str[i]=='o' || str[i]=='u' || str[i]=='A' || str[i]=='E' || str[i]=='I' || str[i]=='O' || str[i]=='U') ++v; else if((str[i]>='a'&& str[i]<='z') || (str[i]>='A'&& str[i]<='Z')) ++c; else if(str[i]>='0'&&c<='9') ++d; else if (str[i]==' ') ++s; } printf("Vowels: %d",v); printf("\nConsonants: %d",c); printf("\nDigits: %d",d); printf("\nWhite spaces: %d",s); }
No. of particular character in a string.
#include <stdio.h> main() { char c[100],ch; int i,count=0; printf("Enter a string: "); gets(c); printf("Enter a character to find frequency: "); scanf("%c",&ch); for(i=0;c[i]!='\0';i++) { if(ch==c[i]) count++; } printf("Frequency of %c = %d", ch, count); }
Prime numbers b/w given interval
/* A C program to display all prime numbers between given interval. */ #include <stdio.h> main() { int n1, n2, i, j, c; printf("Enter two numbers : "); scanf("%d %d", &n1, &n2); printf("Prime numbers between %d and %d are: ", n1, n2); for(i=n1+1;i<n2;i++) { c=0; for(j=2;j<=i/2;j++) { if(i%j==0) { c=1; break; } } if(c==0) printf("%d ",i); } }
Prime Number or not
/* A C program to check whether a number is prime or not. */ #include<stdio.h> main() { int n, i, c=0; printf("Enter the number to check whether this number is prime or not : "); scanf("%d",&n); for(i=2;i<=n/2;i++) { if(n%i==0) { c=1; break; } } if (c==0) printf("%d is a prime number.",n); else printf("%d is not a prime number.",n); }
Friday, 20 February 2015
Fibonacci series recursive
#include<stdio.h> void fibonacci(int); main() { int k,n; long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d",&n); printf("Fibonacci Series: "); printf("%d %d ",0,1); fibonacci(n); } void fibonacci(int n) { static long int first=0,second=1,sum; if(n>0) { sum = first + second; first = second; second = sum; printf("%ld ",sum); fibonacci(n-1); } }
Fibonacci Series
#include<stdio.h> main() { int k,r; int i=1,j=1,f; printf("Enter the number range : "); scanf("%d",&r); printf("fibonacci series :\n"); printf("%d\n%d\n",i,j); for(k=2;k<r;k++) { f=i+j; i=j; j=f; printf("%d\n",j); } }
Reverse string
#include<stdio.h> char* reverse(char[]); int main() { char str[100],*rev; printf("Enter any string: "); scanf("%s",str); rev = reverse(str); printf("Reversed string is: %s",rev); } char* reverse(char str[]) { static int i=0; static char rev[100]; if(*str) { reverse(str+1); rev[i++] = *str; } return rev; }
Reverse the string without library function
#include<stdio.h> #include<string.h> main() { char str[50]; int i,j,len,temp; printf("enter the string\n"); gets(str); len=strlen(str); for(i=0,j=len-1;i<=len/2;i++,j--) { temp=str[i]; str[i]=str[j]; str[j]=temp; } printf("reversed string is : %s\n",str); }
Reverse the string
#include<stdio.h> #include<string.h> main() { char str[50]; printf("enter the string\n"); gets(str); strrev(str); printf("reversed string is : %s\n",str); }
Swapping without temp variable
#include<stdio.h> main() { int num1,num2; printf("Enter number 1 : "); scanf("%d",&num1); printf("Enter number 2 : "); scanf("%d",&num2); printf("Before swapping\n"); printf("number 1 is %d and number 2 is %d\n",num1,num2); num1=num1+num2; num2=num1-num2; num1=num1-num2; printf("After swapping \n"); printf("number 1 is %d and number 2 is %d\n",num1,num2); }
Swapping using temp variable
#include<stdio.h> main() { int num1,num2,temp; printf("Enter number 1 : "); scanf("%d",&num1); printf("Enter number 2 : "); scanf("%d",&num2); printf("Before swapping\n"); printf("number 1 is %d and number 2 is %d\n",num1,num2); temp=num1; num1=num2; num2=temp; printf("After swapping \n"); printf("number 1 is %d and number 2 is %d\n",num1,num2); }
Linear Search
#include<stdio.h> main() { int arr[10],i,n,key,c=0; printf("Enter the size of an array: "); scanf("%d",&n); printf("Enter the elements of the array: "); for(i=0;i<=n-1;i++) { scanf("%d",&arr[i]); } printf("Enter the number to be search: "); scanf("%d",&key); for(i=0;i<=n-1;i++) { if(arr[i]==key) { c=1; break; } } if(c==0) printf("The number is not found"); else printf("The number is found"); return 0; }
Heap Sort
#include<stdio.h> #include<stdlib.h> main() { int heap[50],i,j,ch,size,loc,par; int temp; printf("enter the size of heap tree\n"); scanf("%d",&size); printf("all the elements\n"); for(i=0;i<size;i++) scanf("%d",&heap[i]); for(i=0;i<size;i++) { loc=i; do { par=(loc-1)/2; if(heap[par]<heap[loc]) { temp=heap[par]; heap[par]=heap[loc]; heap[loc]=temp; } loc=par; }while(loc!=0); } printf("heap tree is \n"); for(i=0;i<size;i++) printf("%d\t",heap[i]); for(j=size-1;j>=0;j--) { temp=heap[0]; heap[0]=heap[j]; heap[j]=temp; par=0; do { ch=2*par+1; if(heap[ch]<heap[ch+1]&&ch<j-1) ch++; if(heap[par]<heap[ch]&&ch<j) { temp=heap[par]; heap[par]=heap[ch]; heap[ch]=temp; } par=ch; }while(ch<j); } printf("\nsorted array\n"); for(i=0;i<size;i++) printf("%d\t",heap[i]); }
Binary search
#include<stdio.h> main() { int arr[100],i,n,key,c=0,first,last,mid; printf("Enter the number of element in array : "); scanf("%d",&n); printf("Enter the elements in ascending order : "); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } printf("Enter the number to be search : "); scanf("%d",&key); first=0; last=n-1; while(first<=last) { mid=(first+last)/2; if(key==arr[mid]) { c=1; break; } else if(key<arr[mid]){ last=mid-1; } else first=mid+1; } if(c==0) printf("The number is not found."); else printf("The number is found."); return 0; }
Binary search ( Recursion )
#include<stdio.h> int binary(int [],int,int,int,int); main() { int a[50],i,n,m,first,last,c; printf("enter the number of elements\n"); scanf("%d",&n); printf("enter all elements\n"); for(i=0;i<n;i++) scanf("%d",&a[i]); printf("enter the number to be searched\n"); scanf("%d",&m); first=0; last=n-1; c=binary(a,n,m,first,last); if(c==0) printf("element NOT FOUND\n"); else printf("element found\n"); } int binary(int a[],int n,int m, int first, int last) { int mid,c=0; if(first<=last) { mid=(first+last)/2; if(m==a[mid]) { c=1; } else { if(m<a[mid]) return binary(a,n,m,first,mid-1); else return binary(a,n,m,mid+1,last); } } return c; }
Quick sort on String
/* A c program to perform quick sort on String */ #include<stdio.h> #include<string.h> #include<ctype.h> void quick(char [],int,int); main() { int first,last,i,j,n; char a[50]; printf("enter string\n"); gets(a); n=strlen(a); quick(a,0,n-1); printf("new string\n"); printf("%s",a); } void quick(char a[],int first,int last) { int i,j,pivot,temp; if(first<last) { pivot=first; i=first; j=last; while(i<j) { while(tolower(a[i])<=tolower(a[pivot])&&i<last) i++; while(tolower(a[j])>tolower(a[pivot])) j--; if(i<j) { temp=a[i]; a[i]=a[j]; a[j]=temp; } } temp=a[pivot]; a[pivot]=a[j]; a[j]=temp; quick(a,first,j-1); quick(a,j+1,last); } }
Tower Of Hanoi
#include<stdio.h> #include<conio.h> int tower(int,char,char,char); main() { char source,temp,dest; int num; printf("ENTER THE NUMBER OF DISK FOR 'TOWER OF HANOI'\n"); scanf("%d",&num); tower(num,'A','B','C'); } int tower(int num,char source,char temp,char dest) { if(num==1) { printf("move %d from %c to %c\n\n",num,source,dest); } else { tower(num-1,source,dest,temp); printf("move %d from %c to %c\n\n",num,source,dest); tower(num-1,temp,source,dest); } }
Subscribe to:
Posts (Atom)