-3

user input:
string[0]="My Address Street";
string[1]="My Street Address";
string[2]="Street Address";

strstr(string[i]," Street")
// return 0 for string[2], because there is no whitespace before `Street` word

I want to make user input Street only at last string, not in the middle or first string use strstr();.

some trick using reverse string, then count until meet space, then reverse back to normal and substring [ ( string length - count ), (string length) ]. And then compare it with the required input ('Street' at the end of inputted string). if match -> pass or looping until match.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • If the substring is found by `strstr` the returned pointer will point to that substring. So look at the character just beyond that substring, with say `subptr[strlen(" Street")]`, and verify it is the nul string terminator or the newline (if input was from `fgets`). – Weather Vane Apr 02 '17 at 19:24
  • What would your algorithm do when given "Orchard Road, Street" (a town near Glastonbury, England)? – Weather Vane Apr 02 '17 at 19:38

1 Answers1

0

thanks, i use this.

char address[50];
do{
    printf("> ");
    scanf("%[^\n]",address);
}while(strstr(address," Street")==0 || strstr(address,"Street ")!=0);
/* user input:
> Orchard Road        
> Street              
> Street Orchad Road
> Orchad Street Road
> Orchad Road, Street
*/
  • [You should not _ever_ use `gets()` for anything](http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used). It is a dangerous function, deprecated in C99 and completely removed from the language in C11. Use `fgets()` or the POSIX `getline()` instead. – ad absurdum Apr 02 '17 at 20:58
  • thank you, i just starting in c, i will learn more about that. – Fran Sudarojat Apr 02 '17 at 21:13