Wednesday, December 19, 2012

FIND A CHARACTER POSITION AND INDEX OF A STRING IN C++


#include<iostream.h>
void main()
{
int findpos(char s[],char c);
char string[80],ch;
int y=0;
cout<<"\nENTER MAIN STRING :\n";
cin.getline(string, 80);
cout<<"\nENTER CHARACTER TO BE SEARCH FOR : ";
cin.get(ch);
y=findpos(string,ch);
if(y==-1)
cout<<"\nSORRY!! THE CHARACTER IS NOT IN STRING.";
}
int findpos(char s[], char c)
{
int flag=-1;
for(int i=0; s[i]!='\0';i++)
{
if(s[i]==c)
{
flag=0;
cout<<"\nTHE CHARACTER IN THE STRING IS AT INDEX IS  : "<<i;
cout<<"\nTHE CHARACTER IN THE STRING IS AT POSITION : "<<++i;

}
 }
return (flag);
}

No comments: