Having problem in User Defined function which works like strstr function

This is a discussion on Having problem in User Defined function which works like strstr function within the C++ Programming forums, part of the General Programming Boards category; Hello, this is my yet another problem i have made 1 user defined function which works pretty much like strstr ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    225

    Having problem in User Defined function which works like strstr function

    Hello,
    this is my yet another problem i have made 1 user defined function which works pretty much like strstr function defined in string.h but it's having problem. I have figured out the problem but don't know how to solve it. Here is the code! Please have a look and tell me how to solve it?

    Code:
    #include <iostream.h>
    char* ustrstr(char s[],char t[])
    {
     int i,j;
     char *temp;
     for(i=0;s[i]!=NULL;i++)
     {
       j=0;
    	temp=&s[i];
    	while(t[j]==s[i] && s[i]!=NULL)
    	{
    		j++;
    		i++;
    	}
    	if(t[j]==NULL)
    		return temp;
    	if(s[i]==NULL)
    		i--;
     }
     return NULL;
    }
    
    int main(void)
    {
     char s[100],*j;
     cout<<"\n\nEnter Any String :";
     cin.getline(s,100);
    
     j=ustrstr(s,"technology");
     if(j==NULL)
    	cout<<"\n\nSubstring Not Found";
     else
    	cout<<"\n\nSubstring Found :"<<j;
    return 0;
    }
    Just give input as iliketechnology it works fine and then give ilikettechnology! it's not working i know where the problem is but dont know how to solve. Please help me

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Awfully complicated. Bad variable names.
    Plus remember that NULL is only associated with pointers. What you should use is 0 or '\0', not NULL.
    Code:
    char* FindSubStr(char* pString, char* pFind)
    {
    	std::size_t OriginalStrLength = strlen(pString);
    	std::size_t FindStrLength = strlen(pFind);
    	for (std::size_t i = 0; i < OriginalStrLength; i++)
    	{
    		// Length of the string to find exceeds the length of the original
    		// string from here on, so it's impossible to find it.
    		if (OriginalStrLength - i < FindStrLength)
    			return NULL;
    		if (pString[i] == pFind[0])
    		{
    			std::size_t PosOriginal = i, PosFind = 0;
    			for (; PosFind < FindStrLength && (pString[PosOriginal++] == pFind[PosFind++]););
    			if (PosFind == FindStrLength) return &pString[i];
    		}
    	}
    	return NULL;
    }
    This one is far easier to read.

    And why reinvent the wheel? Homework? Exercise?
    Last edited by Elysia; 03-02-2008 at 09:05 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Elysia your code is worse than me!Firstly you can't write if condition in the way that you have written in for loop secondly you have written (pString[j] == pFind[j])??? what does this mean elysia??we need to check the pstring[i] with pfind[j]
    so you can imagine!!

    secondly your function does not produce any output whatsoever!

    Also elysia the meaning of 0, \0 and NULL are all same NULL is the macro defined in iostream.h may be..i m not sure but it's a macro and its same as 0
    Last edited by chottachatri; 03-02-2008 at 08:51 AM.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Ehh, sorry for the typos and such. I should test the code a little...
    I know the function didn't work right away. All this is difficult to write without some testing. But have a look again. I updated it with tested & working code.
    Yes, 0, '\0' and NULL are the same, but that doesn't mean they mean the same.
    NULL is used to check for pointers that point nowhere or set a pointer to point nowhere.
    '\0' is the character used for the terminating of strings. It's usually the same as 0.
    Last edited by Elysia; 03-02-2008 at 08:58 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok thanks elyii i have solved it myself!this one's working perfectly now only little mistake was there

    Code:
    char* ustrstr(char s[],char t[])
    {
     int i,j;
     char *temp;
     for(i=0;s[i]!=NULL;i++)
     {
       j=0;
    	temp=&s[i];
    	while(t[j]==s[i] && s[i]!=NULL)
    	{
    		j++;
    		i++;
    	}
    	if(t[j]==NULL)
    		return temp;
    	else
    		i=i-j;
    	if(s[i]==NULL)
    		i--;
     }
     return NULL;
    }

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Yes, it's always some subtle mistake, but I'm telling you your code isn't easy to read. Bad variable names and more complicated code than needs be.
    Take heed from my example, which is also safer and probably faster, as well.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    well i am comfortable with my code only thanks anyways!

    and ya i think you only argued few days back if am not wrong that arrays and pointers are same so by your theory NULL can also be used in character type arrays so what's the worry in that?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Yes, that's true, but when you read code, you expect NULL to be used on pointers and no in character arrays.
    Character arrays are terminated by '\0'.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    Ok that's absolutely correct and what does NULL mean then?does it not mean 0 or \0?
    NULL is the macro and it means the same anyways thanks for your code

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    It works, but it doesn't mean it's right.
    NULL is defined as a "NULL pointer", a pointer that points nowhere.
    And if sometime, somewhere, character arrays were to stop being terminated by 0 and you check for NULL, then you would have problems.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    225
    But pointers and arrays are same (in your theory) then why to worry?and i am not getting any such problems which you stated earlier

    Thanks for your code and let's stop arguing!

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    No, no. I don't think you understand the point.
    Even if arrays and pointers are the same, this is still wrong.
    Setting a pointer to NULL, ensures it does not point anywhere.
    So
    Code:
    char* p = NULL;
    p point nowhere.
    However, we're not discussing the address here.
    Code:
    char* p = new char;
    *p = 0;
    delete p;
    p = NULL;
    See the code above. *p is not NULL and will never be.
    However, p is NULL. The pointer is NULL, not the value.
    Code:
    char str[] = "My string";
    This character array has the values 'M', 'y', ' ', 's', 't', 'r', 'i', 'n', 'g' and '\0'. Nut not NULL.
    I can sett NULL to the pointer itself, but not as a value.
    (So you shouldn't do
    Code:
    char str[] = "My string";
    str[2] = NULL;
    Though it's valid, str[2] is not a pointer, it's a char, and therefore, it should not be assigned NULL.
    NULL is for pointers only, to tell that the pointer points nowhere.)

    I'm not sure if you understand the point...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,725
    NULL is defined as a "NULL pointer", a pointer that points nowhere.
    And if sometime, somewhere, character arrays were to stop being terminated by 0 and you check for NULL, then you would have problems.
    hmm... that might not be correct. In C++, NULL is zero. '\0' is the char with a value of 0. Therefore, I reason that NULL == '\0' will always hold true. On the other hand, we consider NULL to be the value of a null pointer, and '\0' is a char literal, so semantically they are not the same.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Quote Originally Posted by laserlight View Post
    On the other hand, we consider NULL to be the value of a null pointer, and '\0' is a char literal, so semantically they are not the same.
    Which is the point we've been trying to make.
    They may have the same value, but they are not the same thing - different meanings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    In C++, NULL is zero. [...] Therefore, I reason that NULL == '\0' will always hold true.
    No, it's not and it will not. 18.1/4 says:
    The macro NULL is an implementation-defined C++ null-pointer constant in this International Standard.
    It is therefore valid for a compiler to define NULL to be something other than 0, provided that it still is assignable to any pointer type and will result in the null pointer value.

    In particular, take GCC. It has a compiler extension keyword __null. This special keyword is convertible to any null pointer or the integer 0, but on the latter conversion the compiler emits a warning. Thus, given the compiler command line
    g++ -Werror null.cpp
    with null.cpp:
    Code:
    #include <cassert>
    #include <cstdlib>
    
    int main()
    {
      assert(NULL == '\0');
    }
    the assertion will not hold - the program will not compile.

    More to the point, many C++0x implementation will (perhaps optionally) implement NULL as a typedef for nullptr, where the above program will result in an error even without treating warnings as errors.

    NULL is to be treated as a pointer and nothing else. In particular, it is definitely not to be treated as a character.

    By the way:
    Code:
    namespace {
    template <typename Ch>
    Ch *ustrstr_impl(Ch *haystack, Ch *needle)
    {
      for(Ch *ho = haystack; *ho; ++ho) {
        const char *hi, *ni;
        for(hi = ho, ni = needle; *hi && *ni && *hi == *ni; ++hi, ++ni) {
        }
        if(!*ni) return ho;
      }
      return 0;
    }
    }
    const char *ustrstr(const char *haystack, const char *needle)
    {
      return strstr_impl(haystack, needle);
    }
    char *ustrstr(char *haystack, char *needle)
    {
      return strstr_impl(haystack, needle);
    }
    const wchar_t *uwcsstr(const wchar_t *haystack, const wchar_t *needle)
    {
      return strstr_impl(haystack, needle);
    }
    wchar_t *uwcsstr(wchar_t *haystack, wchar_t *needle)
    {
      return strstr_impl(haystack, needle);
    }
    ... because you don't want to carry over C's limitations to your implementation, do you?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 01:28 PM
  3. Problem using "cin" in a thread function?
    By Fossil in forum Windows Programming
    Replies: 4
    Last Post: 11-24-2003, 08:08 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21