Find the answer to your Linux question:
Results 1 to 9 of 9
i tried to write a function that does the same thing as strstr(), except it's case-insensitive.. it compiles fine, but when run it always returns (null)... any clu esa to ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078

    result of program always returns (null)


    i tried to write a function that does the same thing as strstr(), except it's case-insensitive.. it compiles fine, but when run it always returns (null)... any clu esa to what i did wrong? the code is:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    
    void dump_line&#40;FILE *fp&#41;;
    char *strstrci&#40;char *str1, char *str2&#41;;
    
    void dump_line&#40;FILE *fp&#41;
    &#123;
    	int ch;
    	while&#40;&#40;ch=fgetc&#40;fp&#41;&#41;!=EOF&&ch!='\n'&#41;
    		;
    &#125;
    
    char *strstrci&#40;char *str1, char *str2&#41;
    &#123;
    	int count;
    	
    	for&#40;count=0;count<=&#40;strlen&#40;str1&#41;&#41;;count++&#41;
    	&#123;
    		str1&#91;count&#93;=&#40;char&#41;tolower&#40;str1&#91;count&#93;&#41;;
    		if&#40;str1&#91;count&#93;=='\n'&#41;
    			str1&#91;count&#93;='\0';
    	&#125;
    
    	for&#40;count=0;count<=&#40;strlen&#40;str2&#41;&#41;;count++&#41;
    	&#123;
    		str2&#91;count&#93;=&#40;char&#41;tolower&#40;str2&#91;count&#93;&#41;;
    		if&#40;str2&#91;count&#93;=='\0'&#41;
    			str2&#91;count&#93;='\0';
    	&#125;
    
    	return &#40;strstr&#40;str1,str2&#41;&#41;;
    &#125;
    
    int main&#40;&#41;
    &#123;
    	char str1&#91;31&#93;;
    	char str2&#91;31&#93;;
    
    	printf&#40;"\nEnter string &#40;30 char max&#41;&#58; "&#41;;
    	fgets&#40;str1,31,stdin&#41;;
    	dump_line&#40;stdin&#41;;
    	printf&#40;"\nEnter text to search for &#40;30 char max&#41;&#58; "&#41;;
    	fgets&#40;str2,31,stdin&#41;;
    	dump_line&#40;stdin&#41;;
    
    	printf&#40;"\nFound&#58; %s\n",&#40;strstrci&#40;str1,str2&#41;&#41;&#41;;
    	
    	return 0;
    &#125;
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  2. #2
    Linux User
    Join Date
    Nov 2002
    Posts
    420
    use the ! operator to check what the inital value is before you run the program, see what happens

  3. #3
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    I am pretty sure it is because you are not dropping your \n's from the user input. Unfortunately I don't know how to fix it.
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  4. #4
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    Found it, see comment below:

    Code:
    * *for&#40;count=0;count<=&#40;strlen&#40;str2&#41;&#41;;count++&#41; 
    * *&#123; 
    * ** *str2&#91;count&#93;=&#40;char&#41;tolower&#40;str2&#91;count&#93;&#41;; 
    * ** *if&#40;str2&#91;count&#93;=='\n'&#41;  // you have '\0' instead of '\n' 
    * ** ** *str2&#91;count&#93;='\0'; 
    * *&#125;
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  5. #5
    Linux User
    Join Date
    Sep 2003
    Posts
    254
    Hi
    I've just a question quite off topic but important: how could I cut and paste this code under vi for example to compile it see the results and try to help u?
    thx

  6. #6
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    grux ... I kept getting something like ?| when I pasted into vi. So I understand what you are asking. So what I did was paste the entire code sample, then copied the first space on one of the indented lines, then pasted it into a %s/// ( ie: %s/<paste here>/ /g )
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  7. #7
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    ah, okay.. thanks sykkn.. i never even saw that '\0'...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

  8. #8
    Linux Newbie
    Join Date
    Aug 2001
    Location
    USA, Texas
    Posts
    217
    ok, got a question for you now. Why is the dump_line needed? If I leave it in I have to push enter twice after entrering the string. If I take it out then I only press enter the one time.
    [ [ SykkN alloc ] initWithThePowerTo: destroyYouAll ];
    /* Don\'t make me use it! */

  9. #9
    Linux Engineer
    Join Date
    Sep 2003
    Location
    Knoxhell, TN
    Posts
    1,078
    i picked it up from a "common c errors" webpage written by some CS professor.. it's used to get rid of extra crap in stdin.. but it only seems to work right *if* the newline char is left in the buffer (i.e. gets(), scanf(), fscanf()).. my friend told me that it's not really all that necessary most of the time.. the reason you have to press enter twice with it is because fgetc() is buffered and is waiting for a \n.. because fgets() doesn't leave the newline, you have to press enter to get fgetc() to exit...
    Their code will be beautiful, even if their desks are buried in 3 feet of crap. - esr

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •