Sie sind auf Seite 1von 3

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#define SIZE 20
char *myStrstr( const char *str, const char *strSearch ); // question 1
char* strstrLastIdx(const char* str,const char* strSearch); // question 2
int strSpecialCmp(const char *str , const char *strSearch); // will help us with
both questions
void main ()
{
char user_menu ; // for the first menu
char str[SIZE], strSearch[SIZE] ; // [] used only twice
int index ;
printf ("Press 1 for question number 1:");
printf ("\n\nPress 2 for question number 2:");
printf ("\n\nPress E or e to exit\n\n");
fflush(stdin);
scanf ("%c",&user_menu); // get user choice
switch (user_menu) // start of user menu
{
case '1': // start of question number 1
{
puts ("Please enter the first string:");
fflush(stdin);
gets( str ); // recevied a chars from user
puts ("\n\nPlease enter the sub string:");
fflush(stdin);
gets( strSearch ); // recevied the sub string
if (*str < 'A'|| *strSearch < 'A') // if user isnt press
ed any char NULL is printed
{
printf ("\n\n\nNULL\n\n");
main(); // back to manu
}
index = strSpecialCmp(str ,strSearch); // first i check
if the substring is included in the string
if ( index == 0) // if false = no matching
printf ("\n\nstrSearch is not found in str\n\n")
;
else // if true , ill used the mystrstr function
printf ("\nLocation found :char %c,idx :%d\n\n",
*strSearch,(int)(myStrstr(str,strSearch)));// casting from char to int
_flushall(); // flush the memory to avoid keeping chars
in memory
main(); // back to main manu
break; // avoid bad loops
}
case '2':
{
puts ("Please enter the first string:");
fflush(stdin);
gets( str ); // recevied a chars from user
puts ("\n\nPlease enter the sub string:");
fflush(stdin);
gets( strSearch );
if (*str < 'A'|| *strSearch < 'A') // if user isnt press
ed any char NULL is printed
{
printf ("\n\n\nNULL\n\n");
main(); // back to manu
}
index =strSpecialCmp(str ,strSearch); //first i check if
the substring is included in the string
if ( index == 0)
printf ("\n\nstrSearch is not found in str\n\n")
;
else
{
printf ("\nLocation found :char %s , idx :%d\n\n",strSea
rch ,(int)(strstrLastIdx(str,strSearch)-str)); //%s for printing the whole strin
g
}
_flushall();
main();
break;
}
case 'e':
case 'E':
{
printf ("\n\nYou choosed to exit , thank you for using m
y program...\n\n");
break;
}
default:
{
printf ("\n\nYou made a wrong choise , please try again:
\n\n");
main() ; // back to main function
break;
}
}
}
int strSpecialCmp(const char *str , const char *strSearch) // return 0 if substr
ing not in string ,and 1 for oposite
{
char *temp_str = str ;// must to do so cause str is const , and strSearc
h is too
char *temp_strS = strSearch ;
if ((*temp_str == 0) && (*temp_strS != 0)) // in case that str is ended
or empty
return 0;
else if ((*temp_str == 0 && *temp_strS == 0)||( *temp_str != 0 && *temp_
strS == 0)) // both ended or just the substring
return 1;
else if(*temp_str == *temp_strS) // check if they equal
return strSpecialCmp (temp_str+1,temp_strS+1); // move one forwa
rd
else // if any other case
return strSpecialCmp (temp_str+1,temp_strS);
}
char *myStrstr( const char *str, const char *strSearch ) // function that will r
eturn str
{
int count = 0 ;
char *temp_str = str ;
char *temp_strS = strSearch ; // str and strSearch is const
if ( *temp_str != *temp_strS )
{
return myStrstr(++temp_str,temp_strS) + (++count); // move temp_
str one cell forward and count
}
else if ( *temp_str == *temp_strS )
{
++temp_str , ++temp_strS ; // first move both one cell forward
{
if ( *temp_str == *temp_strS ) // if still ok back to loop
return myStrstr(temp_str,temp_strS);
else if ( *temp_str <'A' || *temp_strS < 'A') // if cells is ove
r
return (char *)count ; // must return as char* , so made
casting
return myStrstr(temp_str,--temp_strS)+ (++count); // coun
t and move forward
}
}
}
char* strstrLastIdx(const char* str,const char* strSearch)
{
char *temp = str ;
if ( *temp != *strSearch )
{
return strstrLastIdx(temp+1,strSearch);
}
else if ( *temp == *strSearch )
{
if ( strSpecialCmp(temp+1,strSearch) == 1) // if strSpcialCmp still foun
d the substring in the string , continue
return strstrLastIdx(temp+1,strSearch); //
else
return temp ;
}
}

Das könnte Ihnen auch gefallen