Você está na página 1de 34

1.

ARRAY OPERATIONS
void main() { int a[10][10],b[10][10],c[10][10],i,j,k,m,n,ch; clrscr(); printf("Etner the Values of Row and Column"); scanf("%d %d", &m, &n); printf("Enter the Values for Matrix A\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) scanf("%d",&a[i][j]); } printf("Enter the Values for Matrix B\n"); for(i=1;i<=m;i++) { for(j=1;j<=n;j++) scanf("%d",&b[i][j]); } printf("\t\tMatrix Operations\n"); printf("\t\t1. Addition\n"); printf("\t\t2. Subtraction\n"); printf("\t\t3. Multiplication\n"); printf("\t\t4. Transpose\n"); printf("\t\t5. Exit\n"); printf("Enter Your Choice:"); scanf("%d",&ch); switch(ch) { case 1: for(i=1;i<=m;i++) { for(j=1;j<=n;j++) {c[i][j]=a[i][j]+b[i][j]; printf("%d\t",c[i][j]);} printf("\n"); } break; case 2: for(i=1;i<=m;i++) { for(j=1;j<=n;j++) {c[i][j]=a[i][j]-b[i][j]; printf("%d\t",c[i][j]);} printf("\n"); } break; case 3: for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { c[i][j]=0; for(k=1;k<=m;k++) c[i][j]=c[i][j]+a[i][k]*b[k][j]; } } for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { printf("%d\t",c[i][j]);} printf("\n");

} break; case 4: for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { c[i][j]=a[j][i]; printf("%d\t",c[i][j]); } printf("\n"); } break; default: exit(0); } getch(); }

2. ARRANGING NUMBERS IN ASCENDING ORDER


#include <stdio.h> #include <conio.h> void main() { int i,j,n,a[10],t; clrscr(); printf("Sorting\n"); printf("Enter the limit :"); scanf("%d",&n); printf("\nEnter the values:\n"); for (i=1;i<=n;i++) { scanf("%d",&a[i]); } printf("\nSorted values are :\n"); for (i=1;i<=n-1;i++) { for (j=1;j<=n-i;j++) { if (a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } else continue; }/*for j*/ }/*for i*/ for(i=1;i<=n;i++) { printf("%d\n",a[i]); } getch(); }

3.STRING COMPARE WITHOUT USING BUILT-IN FUNCTION (ARRAY)


#include <stdio.h> #include <conio.h> void main()

{ int i=0; char a[15],b[15]; clrscr(); gets(a); gets(b); while(a[i]==b[i]&&a[i]!='\0'&&b[i]!='\0') i++; if (a[i]=='\0'&&b[i]=='\0') printf("Strings are equal"); else printf("Strings are not equal"); getch(); }

4.STRING CONCATENATION WITHOUT USING BUILT-IN FUNCTION (ARRAY)


#include <stdio.h> #include <conio.h> #include <string.h> void main() { char s1[10],s2[10]; int i,j; clrscr(); gets(s1); gets(s2); for (i=0;s1[i]!='\0';i++); for (j=0;s2[j]!='\0';j++) { s1[i]=s2[j]; i++; } s1[i]='\0'; printf("concatenated string is%s\n",s1); printf("length of the new string is %d",i); getch(); }

5.STRING COPY WITHOUT FUNCTION(ARRAY)


#include <stdio.h> #include <conio.h> void main() { int i=0; char s[20],d[20]; clrscr(); printf("Enter the string :"); gets(s); while (s[i]!='\0') { d[i]=s[i]; i++; } d[i]='\0'; printf("Copied String : %s",d); getch(); }

6.ARMSTRONG NUMBER
#include <stdio.h> #include <conio.h> #include <math.h>

void main() { int n,num,d,rev=0,i=3; clrscr(); printf("Enter the value scanf("%d",&num);

:");

n=num; while (n!=0) { d=n%10; /*s+=d;*/ rev=rev+(d*(pow(10,i-1))); printf("rev=%d\n",rev); n=n/10; i--; } /*printf("SUM=%d\n",s);*/ printf("Reverse =%d\n",rev); if (num==rev) printf("Armstrong number"); else printf("Not an Armstrong number"); getch(); }

7.LINKED LIST
#include <stdio.h> #include <malloc.h> struct node { struct node *prev; int info; struct node *next; }*start; main() { int choice,n,m,po,i; start=NULL; clrscr(); while(1) { printf("1.Create List\n"); printf("2.Add at begining\n"); printf("3.Add after\n"); printf("4.Delete\n"); printf("5.Display\n"); printf("6.Count\n"); printf("7.Reverse\n"); printf("8.exit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("How many nodes you want : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter the element : "); scanf("%d",&m);

create_list(m); } break; case 2: printf("Enter the element : "); scanf("%d",&m); addatbeg(m); break; case 3: printf("Enter the element : "); scanf("%d",&m); printf("Enter the position after which this element is inserted : "); scanf("%d",&po); addafter(m,po); break; case 4: printf("Enter the element for deletion : "); scanf("%d",&m); del(m); break; case 5: display(); break; case 6: count(); break; case 7: rev(); break; case 8: exit(0); default: printf("Wrong choice\n"); }/*End of switch*/ }/*End of while*/ }/*End of main()*/ create_list(int num) { struct node *q,*tmp; tmp= malloc(sizeof(struct node)); tmp->info=num; tmp->next=NULL; if(start==NULL) { tmp->prev=NULL; start->prev=tmp; start=tmp; } else { q=start; while(q->next!=NULL) q=q->next; q->next=tmp; tmp->prev=q; } }/*End of create_list()*/ addatbeg(int num) { struct node *tmp; tmp=malloc(sizeof(struct node)); tmp->prev=NULL; tmp->info=num; tmp->next=start; start->prev=tmp;

start=tmp; }/*End of addatbeg()*/ addafter(int num,int c) { struct node *tmp,*q; int i; q=start; for(i=0;i<c-1;i++) { q=q->next; if(q==NULL) { printf("There are less than %d elements\n",c); return; } } tmp=malloc(sizeof(struct node) ); tmp->info=num; q->next->prev=tmp; tmp->next=q->next; tmp->prev=q; q->next=tmp; }/*End of addafter() */ del(int num) { struct node *tmp,*q; if(start->info==num) { tmp=start; start=start->next; /*first element deleted*/ start->prev = NULL; free(tmp); return; } q=start; while(q->next->next!=NULL) { if(q->next->info==num) /*Element deleted in between*/ { tmp=q->next; q->next=tmp->next; tmp->next->prev=q; free(tmp); return; } q=q->next; } if(q->next->info==num) /*last element deleted*/ { tmp=q->next; free(tmp); q->next=NULL; return; } printf("Element %d not found\n",num); }/*End of del()*/ display() { struct node *q; if(start==NULL) { printf("List is empty\n"); return; } q=start;

printf("List is :\n"); while(q!=NULL) { printf("%d ", q->info); q=q->next; } printf("\n"); }/*End of display() */ count() { struct node *q=start; int cnt=0; while(q!=NULL) { q=q->next; cnt++; } printf("Number of elements are %d\n",cnt); }/*End of count()*/ rev() { struct node *p1,*p2; p1=start; p2=p1->next; p1->next=NULL; p1->prev=p2; while(p2!=NULL) { p2->prev=p2->next; p2->next=p1; p1=p2; p2=p2->prev; /*next of p2 changed to prev */ } start=p1; }/*End of rev()*/

8.FUNCTION POINTER
#include<stdio.h> #include<conio.h> void young(int); void old(int); int main(void) { void (*fp)(int); int age; printf("How old are you ?"); scanf("%d",&age); fp=(age>30)?old:young; fp(age); return 0; } void young(int n) { printf("Being only %d sure you are young\n",n); } void old(int m) { printf("Being already %d sure you are old\n",m); }

9.INFIX TO POSTFIX CONVERSION


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

#define size 10 char stack[size]; int tos=0,ele; void push(); char pop(); void show(); int isempty(); int isfull(); char infix[30],output[30]; int prec(char); void main() { int i=0,j=0,k=0,length; char temp; clrscr(); printf("\nEnter an infix expression:"); scanf("%s",infix); printf("\nThe infix expresson is %s",infix); length=strlen(infix); for(i=0;i<length;i++) { //Numbers are added to the out put QUE if(infix[i]!='+' && infix[i]!='-' && infix[i]!='*' && infix[i]!='/' && infix[i]!='^' && infix[i]!=')' && infix[i]!='(' ) { output[j++]=infix[i]; printf("\nThe element added to Q is:%c",infix[i]); } //If an operator or a bracket is encountered... else { if(tos==0) //If there are no elements in the stack, the operator is added to it { push(infix[i]); printf("\nThe pushed element is:%c",infix[i]); } else { //Operators or pushed or poped based on the order of precedence if(infix[i]!=')' && infix[i]!='(') { if( prec(infix[i]) <= prec(stack[tos-1]) ) { temp=pop(); printf("\n the poped element is :%c",temp); output[j++]=temp; push(infix[i]); printf("\n The pushed element is :%c",infix[i]); show(); } else { push(infix[i]); printf("\nThe pushed element is:%c",infix[i]); show(); } } else { if(infix[i]=='(') { push(infix[i]); printf("\nThe pushed-- element is:%c",infix[i]); } if(infix[i]==')') { temp=pop();

while(temp!='(') {output[j++]=temp; printf("\nThe element added to Q is:%c",temp); //temp=pop(); printf("\n the poped element is :%c",temp); temp=pop();} } } } } printf("\nthe infix expression is: %s",output); } while(tos!=0) { output[j++]=pop(); } printf("the infix expression is: %s\n",output); } //Functions for operations on stack void push(int ele) { stack[tos]=ele; tos++; } char pop() { tos--; return(stack[tos]); } void show() { int x=tos; printf("--The Stack elements are....."); while(x!=0) printf("%c, ",stack[--x]); } //Function to get the precedence of an operator int prec(char symbol) { if(symbol== '(') return 0; if(symbol== ')') return 0; if(symbol=='+' || symbol=='-') return 1; if(symbol=='*' || symbol=='/') return 2; if(symbol=='^') return 3; return 0; }

10.INFIX TO POSTFIX
#include<stdio.h> void main() { char stk[30], ex[30],x; int top,i,n; clrscr(); i=1; printf("Expression in infox format. Give @ in the end\n"); do {

x=getchar(); ex[i]=x; i=i+1; } while(x!='@'); n=i-2; printf("The infix expression is\n"); for(i=1;i<=n;i++) printf("%c",ex[i]); i--; printf("%d",i); printf("\nThe postfix expression is\n"); stk[i]='@'; top=1; i=1; in: x=ex[i]; i=i+1; if(x=='@') { while(top>1) { printf("%c",stk[top]); top=top-1; } goto l; } if(x==')') { while(stk[top]!='(') { printf("%c",stk[top]); top=top-1; } top=top-1; goto in; } if((x=='+')||(x=='-')||(x=='*')||(x=='/')||(x=='^')||(x=='(')||(x==')')) { while(isp(stk[top])>=icp(x)) { printf("%c",stk[top]); top=top-1; } top=top+1; stk[top]=x; } else printf("%c",x); goto in; l: getch(); } int isp(x) { switch(x) { case '+': return 1; case '-': return 1; case '*': return 2; case '/':

return 2; case '^': return 3; case '(': return 0; default: return -1; } } int icp(x) { switch(x) { case '+': return 1; case '-': return 1; case '*': return 2; case '/': return 2; case '^': return 4; case '(': return 4; default: return 0; } } 11.STRING LENGTH WITHOUT USING BUILT-IN FUNCTION(ARRAY) #include <stdio.h> #include <conio.h> void main() { int i; char s[20]; clrscr(); printf("Enter the string "); gets(s); for(i=0;s[i]!='\0';i++); printf("Length of the string : %d",i); getch(); } 12. MATRIX MULTIPLICATION #include <stdio.h> #include <conio.h> void main() { int a[5][5],b[5][5],c[5][5]; int i,j,k,m,n,p,q; clrscr(); printf("enter the order of matrix a"); scanf("%d%d",&m,&n); printf("enter the order of matrix b"); scanf("%d%d",&p,&q); if (n!=p)

{ printf("\nMatrix multiplication is not possible"); } else { printf("matrix a\n"); for (i=0;i<m;i++) { for (j=0;j<n;j++) { scanf("%d",&a[i][j]); } } printf("matrix b\n"); for (i=0;i<p;i++) { for (j=0;j<q;j++) { scanf("%d",&b[i][j]); } } printf("\nmatrix c\n"); for (i=0;i<m;i++) { for (j=0;j<q;j++) { c[i][j]=0; for (k=0;k<p;k++) { c[i][j]=c[i][j]+(a[i][k]*b[k][j]); } printf("%d\t",c[i][j]); } printf("\n"); } }/*else*/ getch(); }/*Main*/ 13.FIND ODD OR EVEN USING PASSING POINTER AS FUNCTION ARGUMENT #include <stdio.h> #include <conio.h> void odd_even(int *); void main() { int n; clrscr(); printf("Enter the value "); scanf("%d",&n); odd_even(&n); } void odd_even(int *p) { if (*p%2==0) printf("even"); else printf("odd"); getch(); } 14.POLYNOMIAL ADDITION #include<stdio.h>

#include<conio.h> typedef struct poly { int coeff; int expo; }p; p p1[10],p2[10],p3[10]; void main() { int t1,t2,t3,k; int read(p p1[10]); int add(p p1[10],p p2[10],int t1,int t2,p p3[10]); void print(p p2[10],int t2); void printo(p pp[10],int t2); clrscr(); t1=read(p1); print(p1,t1); t2=read(p2); print(p2,t2); t3=add(p1,p2,t1,t2,p3); printo(p3,t3); getch(); } int read(p p[10]) { int t1,i; printf("\n Enter the total no of terms"); scanf("%d",&t1); printf("\n Enter the coeff and expo in descending order"); for(i=0;i<t1;i++) scanf("%d%d",&p[i].coeff,&p[i].expo); return(t1); } int add(p p1[10],p p2[10],int t1,int t2,p p3[10]) { int i,j,k; int t3; i=0,j=0,k=0; while(i<t1 && j<t2) { if(p1[i].expo==p2[j].expo) { p3[k].coeff=p1[i].coeff+p2[j].coeff; p3[k].expo=p1[i].expo; i++;j++;k++; } else if(p1[1].expo>p2[j].expo) { p3[k].coeff=p1[i].coeff; p3[k].expo=p1[i].expo; i++;k++; } else { p3[k].coeff=p2[j].coeff; p3[k].expo=p2[j].expo; j++;k++; } } while(i<t1) { p3[k].coeff=p1[i].coeff; p3[k].expo=p1[i].expo; i++;k++; } while(j<t2) {

p3[k].coeff=p2[j].coeff; p3[k].expo=p2[j].expo; j++;k++; } t3=k; return t3; } void print(p pp[10],int term) { int k; printf("\n\n Given Polynomial:"); for(k=0;k<term-1;k++) printf("%dx^%d+",pp[k].coeff,pp[k].expo); printf("%dx^%d",pp[k].coeff,pp[k].expo); } void printo(p pp[10],int term) { int k; printf("nn The addition of polynomial:"); for(k=0;k<term-1;k++) printf("%dx^%d+",pp[k].coeff,pp[k].expo); printf("%dx^%d",pp[k].coeff,pp[k].expo); } 15. QUEUE #include<stdio.h> #include<conio.h> #define size 5 int a[size],f=-1,r=-1; int qfull() { if(r>=size-1) return 1; else return 0; } int qempty() { if(f==r) return 1; else return 0; } void insert(int item) { if(qfull()) printf("Queue is Full. Item Cannot be Inserted\n"); else { r++; a[r]=item; } } int delete() { int item; item=a[r]; r--; return item; } void main() { int ch,num,cont,i;

clrscr(); printf("Queue Operations\n"); printf("\n1. Insert \n 2. Delete \n 3. Display do { printf("Enter Your Choice:"); scanf("%d",&ch);

\n 4. Exit\n");

switch(ch) { case 1: printf("Enter the Element To be Inserted:"); scanf("%d",&num); insert(num); break; case 2: if(qempty()) printf("Queue is Empty. No items to Delete\n"); else printf("Item deleted from the queue: %d\n", delete()); break; case 3: if(qempty()) printf("No items in the Queue to display"); else { printf("Items in the Queue\n"); for(i=0;i<=r;i++) printf("%d\n",a[i]); } break; case 4: printf("Program Ends..."); exit(0); } printf("Do you Want to Continue? \n Press 1 to continue. 0 to exit:"); scanf("%d",&cont); }while(cont==1); getch(); } 16.FACTORIAL USING RECURSION #include<stdio.h> #include<conio.h> int n; void main() { clrscr(); printf("\t\tFactorial Calculation using Recursion\n"); printf("Enter the Values of N:"); scanf("%d",&n); printf("FActorial of %d is %d",n,fact(n)); getch(); } int fact(int x) { int f=1; if(x==1) return 1; else { f=x*fact(x-1); return f;

} } 17.FIBONACCI NUMBERS USING RECURSION #include<stdio.h> #include<conio.h> void fibo(int fn, int ln); void main() { clrscr(); printf("Fibonacci Series Using Recursion\n"); fibo(0,1); getch(); } void fibo(int fn,int ln) { printf("%d\n",fn); if(ln<500) fibo(ln,fn+ln); } 18.REVERSE THE STRING WITHOUT FUNCTION(ARRAY) #include <stdio.h> #include <conio.h> void main() { int i,j; char s[15],r[15]; clrscr(); printf("enter the string : "); scanf("%s",s); for(i=0;s[i]!='\0';i++); for(j=i-1,i=0;j>=0;j--) { r[i]=s[j]; i++; } r[i]='\0'; printf("Reverse :%s",r); getch(); } 19.STRING LENGTH USING POINTERS #include <stdio.h> #include <conio.h> void main() { char *s="KartheeswariSaravanan"; int l=0;char *cptr; clrscr(); for(cptr=s;*cptr!='\0';cptr++,l++); printf("length = %d",l); getch(); } 20.STACK OPERATIONS #include<stdio.h> #include<conio.h> #include<stdlib.h> #define size 5

struct stack { int s[size]; int top; }st; int stfull() { if(st.top>=size-1) return 1; else return 0; } int stempty() { if(st.top==-1) return 1; else return 0; } void push(int item) { st.top++; st.s[st.top]=item; } int pop() { int item; item = st.s[st.top]; st.top--; return(item); } void display() { int i; if(stempty()) printf("Stack is Empty"); else { for(i=st.top;i>=0;i--) printf("%d\n",st.s[i]); } } void main() { int item, choice,ans; st.top = -1; clrscr(); printf("Stack Implementation\n"); do { printf("\n 1. Push \n 2. Pop \n 3. Display \n 4. Exit\n"); printf("Enter Your Choice:"); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the item to be pushed:"); scanf("%d",&item); if(stfull()) printf("Stack is Full\n");

else push(item); break; case 2: if(stempty()) printf("Stack is Empty\n"); else { item=pop(); printf("The popped Element is: %d\n",item); } break; case 3: display(); break; case 4: exit(0); } printf("Do you want to continue?"); scanf("%d", &ans); }while(ans==1); getch(); } 21.ARRANGING NAMES IN ASCENDING ORDER #include <stdio.h> #include <conio.h> #include <string.h> void main() { char name[10][20],tmp[20]; int i,j,n; clrscr(); printf("Enter no. of names"); scanf("%d",&n); printf("Enter the names :"); for(i=0;i<=n;i++) gets(name[i]); for (i=0;i<=n;i++) for (j=i+1;j<=n;j++) { if (strcmp(name[i],name[j]) > 0) { strcpy(tmp,name[i]); strcpy(name[i],name[j]); strcpy(name[j],tmp); } } printf("Sorted Names"); for(i=0;i<=n;i++) puts(name[i]); getch(); } 22.SUBSTRING OF A GIVEN STRING #include <stdio.h> #include <conio.h> void substring(int,int,char *);

void main() { char ch[]="where there is a will, there is a way"; ; clrscr(); substring(6,19,ch); getch(); } void substring(int i,int j,char *ch) { printf("substring : %.*s",j-i,&ch[i]); } 23.SUBSTRING USING POINTERS #include <stdio.h> #include <malloc.h> char* substr(char*, int ,int); main() { char str[50]; int *ptr; int pos,len; clrscr(); printf("Enter a string \n"); gets(str); printf("Enter the position and length of substring\n"); scanf("%d%d",&pos,&len); ptr=substr(str,pos,len); printf("Required substring is %s\n",ptr); return 0; } char *substr(char *str,int pos,int len) { char *ptr,*tmp; int c; ptr=malloc(len+1); tmp=ptr; for (c=0;c<pos-1;c++) str++; for(c=0;c<len;c++) { *ptr=*str; ptr++; str++; } *ptr='\0'; ptr=tmp; return ptr; } 24.SWAPPING THE VALUES OF TWO VARIABLES USING POINTERS #include <stdio.h> #include <conio.h> #include <string.h> void main() { int p,q; int swap(int *,int *); clrscr(); printf("Enter the value of p and q"); scanf("%d%d",&p,&q); swap(&p,&q); printf("Exchanged values p=%d q=%d",p,q);

} int swap(int *a,int *b) { int t; t=*a; *a=*b; *b=t; return 1; } 25.TREE TRAVERSAL #include<stdio.h> #include<conio.h> #include<stdlib.h> struct tree { int data; struct tree *lchild,*rchild; }; struct tree *insert(struct tree *p,int n); void inorder(struct tree *p); void preorder(struct tree *p); void postorder(struct tree *p); void main() { int x,y,i; struct tree *root; clrscr(); root=NULL; printf("Enter the no. of nodes in the tree\n"); scanf("%d",&x); while(x-->0) { printf("Enter the data part of the node\n"); scanf("%d",&y); /*root= traverse(root,y);*/ } clrscr(); printf("\t\tEnter the traversal u want\n"); printf("1.Inorder.\n2.Preorder.\n3.Postorder.\n"); scanf("%d",&i); switch(i) { case 1: { printf("The inorder traversal is \n"); inorder(root); } break; case 2: { printf("The preorder traversal is\n"); preorder(root); } break; case 3: { printf("The postorder traversal is\n"); postorder(root); } break; } getch(); }

//function definition for insertion struct tree *traverse(struct tree *p,int n) { static struct tree *temp1,*temp2; if(p==NULL) { p=(struct tree *)malloc(sizeof(struct tree)); p->data=n; p->lchild=p->rchild=NULL; } else { temp1=p; while(temp1!=NULL) { temp2=temp1; if(n<temp1->data) temp1=temp1->lchild; else temp1=temp1->rchild; } if(temp2->data>n) { temp2->lchild=(struct tree *)malloc(sizeof(struct tree)); temp2=temp2->lchild; temp2->data=n; temp2->lchild=temp2->rchild=NULL; } else { temp2->rchild=(struct tree *)malloc(sizeof(struct tree)); temp2=temp2->rchild; temp2->data=n; temp2->lchild=temp2->rchild=NULL; } } return p; } void inorder(struct tree *p) { if(p!=NULL) { inorder(p->lchild); printf("%d ",p->data); inorder(p->rchild); } } void preorder(struct tree *p) { if(p!=NULL) { printf("%d ",p->data); preorder(p->lchild); preorder(p->rchild); } } void postorder(struct tree *p) { if(p!=NULL) { postorder(p->lchild);

postorder(p->rchild); printf("%d ",p->data); } } 26. FILE COPYING USING COMMAND LINE ARGUMENTS #include<stdio.h> int main(int argc,char *argv[]) { FILE *fp1,*fp2; char ch ; fp1 = fopen(argv[1],"r"); if(fp1 == NULL) { printf("Error in opening the file\n"); exit(0); } fp2 = fopen(argv[2],"w"); if(fp2 == NULL) { printf("Error in opening the file\n"); exit(0); } while((ch = getc(fp1))!=EOF) //Copying the contents of src.txt to dst.txt { putc(ch,fp2); } fclose(fp1); fclose(fp2); }

STRING FUNCTIONS

1.Reverse the string #include <stdio.h> #include <conio.h> void main() { int i,j; char s[15],r[15]; clrscr(); printf("enter the string : "); scanf("%s",s); for(i=0;s[i]!='\0';i++); for(j=i-1,i=0;j>=0;j--) { r[i]=s[j]; i++; } r[i]='\0';

printf("Reverse :%s",r); getch(); } 2. String Compare #include <stdio.h> #include <conio.h> void main() { int i=0; char a[15],b[15]; clrscr(); gets(a); gets(b); while(a[i]==b[i]&&a[i]!='\0'&&b[i]!='\0') i++; if (a[i]=='\0'&&b[i]=='\0') printf("Strings are equal"); else printf("Strings are not equal"); getch(); } 3. String concatenation #include <stdio.h> #include <conio.h> #include <string.h> void main() { char s1[10],s2[10]; int i,j; clrscr(); gets(s1); gets(s2); for (i=0;s1[i]!='\0';i++); for (j=0;s2[j]!='\0';j++) { s1[i]=s2[j]; i++; } s1[i]='\0'; printf("concatenated string is%s\n",s1); printf("length of the new string is %d",i);

getch(); } 4.String Length #include <stdio.h> #include <conio.h> void main() { int i; char s[20]; clrscr(); printf("Enter the string "); gets(s); for(i=0;s[i]!='\0';i++); printf("Length of the string : %d",i); getch(); } 5. String Copy #include <stdio.h> #include <conio.h> void main() { int i=0; char s[20],d[20]; clrscr(); printf("Enter the string :"); gets(s); while (s[i]!='\0') { d[i]=s[i]; i++; } d[i]='\0'; printf("Copied String : %s",d); getch(); }

1.String copy using pointers void strcpy(char *s, char *t) { while(*t != '\0') *s++ = *t++; *s = '\0'; } 2.String compare using pointers main() { char compare_function(); int result; char inputA[20],inputB[20]; printf(\nEnter string A : ); gets(inputA); printf(\nEnter string B : ); gets(inputB); result = compare_function(inputA,inputB); if (result == 0) printf(\nStrings are same); else printf(\nStrings are different); } int compare_function(char *a,char *b) { while (*a != \0) { if (*a == *b) { a++; b++; } else return 1; } return 0; }

3.String concatenation using pointers #include<stdio.h> int main(){ int i=0,j=0; char *str1,*str2,*str3; puts("Enter first string"); gets(str1); puts("Enter second string"); gets(str2); printf("Before concatenation \n"); puts(str1); puts(str2); while(*str1){ str3[i++]=*str1++; } while(*str2) { str3[i++]=*str2++; } str3[i]='\0'; printf("After concatenation \n"); puts(str3); return 0; } 4.Reverse the string using pointers #include<stdio.h> int main(){ char str[50]; char rev[50]; char *sptr = str; char *rptr = rev; int i=-1; printf("Enter any string : "); scanf("%s",str); while(*sptr) { sptr++; i++; }

while(i>=0){ sptr--; *rptr = *sptr; rptr++; --i; } *rptr='\0'; printf("Reverse of string is : %s",rev); return 0; } 5. String Length using pointers #include <stdio.h> #include <conio.h> #include <string.h> void main() { char *s=SAMPLE; char *cptr; int len=0; for (cptr=s;*cptr!=\0;cptr++;len++); printf(Length of the string is %d,len); }

Write a C program to count the lines, words and characters in a given text.
* Write a C program to count the lines, words and characters in a given text*/ Program #include <stdio.h> main() { char line[81], ctr; int i,c, end = 0, characters = 0, words = 0, lines = 0; printf(KEY IN THE TEXT.\n); printf(GIVE ONE SPACE AFTER EACH WORD.\n); printf(WHEN COMPLETED, PRESS RETURN.\n\n); while( end == 0) { /* Reading a line of text */ c = 0;

while((ctr=getchar()) != \n) line[c++] = ctr; line[c] = ; /* counting the words in a line */ if(line[0] == ) break ; else { words++; for(i=0; line[i] != ;i++) if(line[i] == || line[i] == \t) words++; } /* counting lines and characters */ lines = lines +1; characters = characters + strlen(line); } printf (\n); printf(Number of lines = %d\n, lines); printf(Number of words = %d\n, words); printf(Number of characters = %d\n, characters); } Output KEY IN THE TEXT. GIVE ONE SPACE AFTER EACH WORD. WHEN COMPLETED, PRESS RETURN. Admiration is a very short-lived passion. Admiration involves a glorious obliquity of vision. Always we like those who admire us but we do not like those whom we admire. Fools admire, but men of sense approve. Number of lines = 5 Number of words = 36 Number of characters = 205 #include < stdio.h > main() { char line[81], ctr; int i,c, end = 0, characters = 0, words = 0, lines = 0; printf("KEY IN THE TEXT.\n"); printf("GIVE ONE SPACE AFTER EACH WORD.\n"); printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n"); while( end == 0) { /* Reading a line of text */ c = 0; while((ctr=getchar()) != '\n') line[c++] = ctr;

line[c] = '\0'; /* counting the words in a line */ if(line[0] == '\0') break ; else { words++; for(i=0; line[i] != '\0';i++) if(line[i] == ' ' || line[i] == '\t') words++; } /* counting lines and characters */ lines = lines +1; characters = characters + strlen(line); } printf ("\n"); printf("Number of lines = %d\n", lines); printf("Number of words = %d\n", words); printf("Number of characters = %d\n", characters); }

#include<string.h> #include<stdio.h> main() { char line[81], ctr; int i,c, end = 0, characters = 0, words = 0, lines = 0; printf("KEY IN THE TEXT.\n"); printf("GIVE ONE SPACE AFTER EACH WORD.\n"); printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n"); while( end == 0) { /* Reading a line of text */ c = 0; while((ctr=getchar()) != '\n') line[c++] = ctr; line[c] = '\0'; /* counting the words in a line */ if(line[0] == '\0') break ; else { words++; for(i=0; line[i] != '\0';i++) if(line[i] == ' ' || line[i] == '\t') words++; } /* counting lines and characters */ lines = lines +1; characters = characters + strlen(line); }

printf ("\n"); printf("Number of lines = %d\n", lines); printf("Number of words = %d\n", words); printf("Number of characters = %d\n", characters); }

1. # include <stdio.h> 2. # include <string.h> 3. # include <ctype.h> 4. int static words = 0,chars = 0,senten = 0; 5. int i = 0; 6. 7. main()/*start of main function*/ 8. { 9. 10. 11. 12. 13. 14. 15. 16. { 17. 18. 20. char ch; char ch1[1000]; FILE*fp; FILE*fp1; fp1 = fopen("NUMBERofCHars.txt","w"); fp = fopen("Paragraph.txt","r+"); while((ch=fgetc(fp)) != EOF) ch1[i] = ch; if (ch1[i] == '.') senten = senten + 1;

19. {/*condition for number of sentences*/

21. 22. 24. 25. 26.

} if (ch1[i] != ' ') chars = chars + 1; } if (ch1[i] == ' ')

23. {/*Condition for number of letters*/

27. {/*condition for number of words*/ 28. words = words + 1; 29. 30. 31. 32. 33. 34. 35. 36. 37. } } i++; } fprintf(fp1,"Number of words : %d\n",words); fprintf(fp1,"Number of letters: %d\n",chars); fprintf(fp1,"Number of sentence: %d\n",senten); fclose(fp); fclose(fp1);

38. }/*End of main*/

File copying using command line args //Create two files src.txt and dst.txt . Put some data is src.txt #include<stdio.h> int main(int argc,char *argv[]) { FILE *fp1,*fp2; char ch ; fp1 = fopen(argv[1],"r"); if(fp1 == NULL) { printf("Error in opening the file\n"); exit(0); } fp2 = fopen(argv[2],"w"); if(fp2 == NULL) { printf("Error in opening the file\n"); exit(0); }

while((ch = getc(fp1))!=EOF) //Copying the contents of src.txt to dst.txt { putc(ch,fp2); } fclose(fp1); fclose(fp2); }

FILE COPY USING FUNCTION


#include <stdio.h> #include <stdlib.h> void filecopy(FILE *a, FILE *b) { char c; while((c=getc(a))!=EOF) putc(c,b); } int main(int argc, char **argv) { FILE* a; FILE* b; if ((a=fopen(*(argv+1),"r"))!=NULL) { if ((b=fopen(*(argv+2),"w"))!=NULL) { filecopy(a,b); } else printf("File error !\n"); } else printf("File error !\n"); fclose(a); fclose(b); system("pause"); } FILE COPY #include<stdio.h> void main(int argc,char *argv[]) { char ch; FILE *src,*tar; if(argc!=3) { printf("Insufficient Parameters !"); exit(0); } src=fopen(argv[1],"r"); if(src==NULL)

{ printf("Cannot find the source file !"); fclose(src); exit(0); } tar=fopen(argv[2],"w"); if(tar==NULL) { printf("Cannot open target file !"); fclose(tar); exit(0); } while(1) { ch=fgetc(src); if(ch==EOF) break; else fputc(ch,tar); } fclose(tar); fclose(src); printf("File Copied Successfully !"); }

Finding the kth smallest element in an unsorted array #include<stdio.h> #include<stdlib.h> #define MAX 10 int find(int ar[],int low,unsigned int high,int k); int length(int ar[]); int main() { int ar[MAX],k,ans; register int i=0; printf("\nPlease enter the numbers in the array"); while(i<MAX) { scanf("%d",&ar[i]); i++; } printf("\nPlease enter the value of k "); scanf("%d",&k); ans=find(ar,0,MAX-1,k); printf("\nThe %d smallest element in the array is %d",k,ans); return 0; } int find(int ar[],int low,unsigned int high,int k) { int mid; mid=(low+high)/2; unsigned int i,j; int p[MAX],q[MAX]; for(i=0,j=0;i<length(ar);i++) { if(ar[i]<ar[mid]) { p[j]=ar[i]; j++; } } for(i=0,j=0;i<length(ar);i++) { if(ar[i]>ar[mid]) { q[j]=ar[i]; j++; } } if(length(p)>=k) { find(p,0,length(p)-1,k); } else if(length(p)==k-1) { return (ar[mid]); } else { find(q,0,length(q)-1,k-mid+1); } } int length(int ar[]) { return(sizeof(ar)/4); }

Você também pode gostar