u

Friday 14 September 2012

C++ Program to input any string and print the number of spaces stored within the string at prime positions

C++ Program to input any string and print the number of spaces stored within the string at prime positions

 #include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
main()
{
char a,name[50];
int f=0, l=0,c=0,k=0;
cout<<"Enter any string "<<endl;
gets(name);
l = strlen(name);
cout<<"The length of the string is "<<l;
cout<<endl;
for(int i=0;i<l;i++)
{
a=name[i];
if (a==' ')
// checking if i is prime
k=i+1;
for(int l=1;l<=k;l++)
{
if (k%l==0)
f=f+1;
}
if (f==2)
c = c + 1;
f=0;
}
cout<<endl<<"The no. of spaces at prime position is  "<<c;
getch();
return 0;

}

0 comments:

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More