C++ program to swap two numbers using pointers

#include<iostream.h> #include<conio.h> void main() { clrscr(); int *a,*b,*temp; cout<<“Enter value of a and b:”; cin>>*a>>*b; temp=a; a=b; b=temp; cout<<“nAfter swapingna=”<<*a<<“nb=”<<*b; getch(); }

C program to print following triangle:

#include<stdio.h> #include<conio.h> void main() { int i,j,k,n,a,b; clrscr(); printf(“How many lines?”); scanf(“%d”,&n); n=n*2; for(i=1;i<n;i+=2) { for(j=n-1;j>i;j-=2) printf(” “); for(k=1;k<=(i/2)+1;++k) printf(“%d”,k); for(k=i/2;k>=1;–k) printf(“%d”,k); printf(“n”); } getch(); }

C program to check whether given string is palindrome or not

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,flag=1,len; char str[50]; clrscr(); printf(“Enter any string:”); gets(str); len=strlen(str); for(i=0,j=len-1;i<len/2;++i,–j) if(str[i]!=str[j]) { flag=0; break; } if(flag) printf(“String is palindrome“); else printf(“String is not palindrome”); getch(); }

C program to reverse a string

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,n; char a[30]; clrscr(); printf(“Enter any string:”); gets(a); n=strlen(a); printf(“Reverse of string:”); for(i=(n-1);i>=0;–i) printf(“%c”,a[i]); getch(); }

C program to find length of a string

#include<stdio.h> #include<conio.h> void main() { int i; char str[50]; clrscr(); printf(“Enter a string:”); gets(str); for(i=0;str[i]!=’’;++i); printf(“Lenth of string is %d”,i); getch(); }

C program to read a string and print it in alphabetical order

#include<stdio.h> #include<conio.h> #include<string.h> void main() { int i,j,n,ch1,ch2; char a[50],temp; clrscr(); printf(“Enter any string:”); scanf(“%s”,a); n=strlen(a); for(i=1;i<n;++i) for(j=0;j<(n-i);++j) { ch1=a[j]; ch2=a[j+1]; if(ch1>ch2) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } printf(“String after arranging %s”,a); getch(); }