return the given char position in any given string n last position

#include<iostream>
using namespace std;
int main ()
{
char str[80],ch;
cout<<“\n enter the string :”;
cin.get(str,80);
cout<<“\n enter the char:->”;
cin>>ch;
int i=0;
while(str[i]!=”)
{
if(str[i]==ch)
{ cout<<i<<” “; i++;
continue; }

i++;
}
cout<<endl<<endl;
system(“pause”);
return 0;
}

//few person ask to return the last position of that particular char in any given string…so just modify lil bit n don print cout<<i<<” “; see below..

//it won work if the given input started from that char like kkkk…. n char=k; //it’ll return 0

#include<iostream>
#include<string.h>
using namespace std;
int main ()
{
char str[80],ch,count[30];
cout<<“\n enter the string :”;
cin.get(str,80);
cout<<“\n enter the char:->”;
cin>>ch;
int i=0,j=0;
while(str[i]!=”)
{
if(str[i]==ch)
{ count[j]=i; j++;
i++;
continue; }
i++;
}
int c=strlen(count);
cout<<(int)count[c-1]; //(int)for showing the value otherwise it’ll show address cout<<endl<<endl;
system(“pause”);
return 0;
}

//another method beter than previous 

#include<iostream>
using namespace std;
int main ()
{
char str[30],ch;
cout<<“\n enter the string :”;
cin.get(str,30);
cout<<“\n enter the char:->”;
cin>>ch;
int i=0,z=0;
while(str[i]!=”)
{
if(str[i]==ch)
{   z=i;
i++; continue; }
i++;
}
cout<<z;    //it’ll print tha last index of that char in given  string
cout<<endl<<endl;
system(“pause”);
return 0;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: