Hey guys,so ive my school exams next week and I was practising pointers in my computer.Heres what the program is supposed to do:-
Program to search for a given character inside a string and to print the string
from the point of the match.The function should return nonzero if true,zero if false.
What Im getting confused about is the if statement.Also,the function match() doesnt return anything at all.I was having more errors before,but now I have managed to compile the program.
#include<iostream>
#include<ctype.h>
#include<string.h>
usingnamespace std;
char *match(char,char*);
int main()
{
char str[80],ch,*p;
cout<<"Enter the char to search: ";
cin>>ch;
cout<<endl;
cout<<"Enter a string (max. 80 chars): ";
cin>>str;
cout<<endl;
p=NULL;
p=match(ch,str);
if(!*p)
{
cout<<"Found the character !!!\n";
for(char *count=p;*count!='\0';count++)
cout<<*count<<endl;
}
elseif(*p)
cout<<"Not found !!!\n";
cout<<"Thank you !!!\n";
return 0;
}
//Neel is a son of a bitch.
//Enter a character: s
char *match(char a,char *str)
{
while((a!=*str) && (*str))
str++;
return (str);
}
Pls help in in answering these two questions.
Thnx in advance :)
Pls run the code and try for yourself guys.Im trying to understand it myself,and would be much obliged if you guys help me in pointing out where in the code Im wrong.
But what I kno till now is that cin>>str; will input the text into str.