C program to count number of words in a string

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

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(); }