Thread: functions with char pointer

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    7

    functions with char pointer

    Hi i need help please,
    the main program below uses a ”helper” function substr() to determine and report if the secondstring that the user enters is ”contained in” the first string. I need to use a nested for loop for this. So far, the program reports certain characters from the second string as false even if they are contained. I do not know what to add or modify to get the program to accurately report all given characters.

    Code:
    #include <stdio.h>
    #include <string.h>
    // returns the starting index of the first such occurrence
    // where sub_str was found within str - if one exists
    // otherwise returns -1
    int substr(const char str[], const char sub_str[]);
    int main()
    {
    	char str[255];
    	printf("Enter a string: "); fflush(stdout);
    	scanf("%s", str);
    	char sub_str[255];
    	do
    	{
    		printf("Enter a string to search for (\"quit\" to quit): "); fflush(stdout);
    		scanf("%s", sub_str);
    		if ( strcmp(sub_str, "quit") )
    		{
    			int ind = substr(str, sub_str);
    			if ( ind == -1 )
    			{
    				printf("Not found: %s\n", sub_str);
    			}
    			else
    			{
    				printf("\"%s\" found at starting index %d\n", sub_str, ind);
    			}
    		}
    	} while ( strcmp(sub_str, "quit") );
    	printf("Done!\n");
    	return 0;
    }
    int substr(const char str[], const char sub_str[])
    {
    	int i, j, big = strlen(str), small = strlen(sub_str), flag = 0;
    
    
    	for(i=0; i<(big-small); i++)
    	{
    		for(j=0; j<small; j++)
    		{
    			if(str[i+j]!=sub_str[j])
    			{
    				 i+=1;
    			}
    			else
    			{
    				flag = 1;
    			}
    
    
    		}
    			if (flag)
    
    
    				return i;
    			else
    			{
    				i++;
    			}
    	}
    			return -1;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    So what's an example of a test case that you think should pass, but actually fails?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    7
    For example if the first string is 'burger' and the second string is 'b' it should report 0, which is the index of the first occurrence in the first string. But 'u' reports not found instead of 1 . And it does the same with some of the other characters.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    Well you could step through the code with a debugger.

    Like I did.
    Code:
    $ gcc -g foo.c
    $ gdb -q ./a.out
    Reading symbols from ./a.out...done.
    (gdb) b substr
    Breakpoint 1 at 0x4008b9: file foo.c, line 33.
    (gdb) run
    Starting program: ./a.out 
    Enter a string: burger
    Enter a string to search for ("quit" to quit): u
    
    Breakpoint 1, substr (str=0x7fffffffdc50 "burger", sub_str=0x7fffffffdd50 "u") at foo.c:33
    33	  int i, j, big = strlen(str), small = strlen(sub_str), flag = 0;
    (gdb) n
    35	  for (i = 0; i < (big - small); i++) {
    (gdb) 
    36	    for (j = 0; j < small; j++) {
    (gdb) 
    37	      if (str[i + j] != sub_str[j]) {
    (gdb) 
    38	        i += 1;
    (gdb) 
    36	    for (j = 0; j < small; j++) {
    (gdb) 
    43	    if (flag)
    (gdb) 
    46	      i++;
    (gdb) 
    35	  for (i = 0; i < (big - small); i++) {
    (gdb) p i
    $1 = 2
    At which point, you might wonder why i is now 2 rather than 1.
    Because by skipping straight to 2, there is no chance to match 'u' at position 1.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assigning a char value to char pointer doesn't work.
    By inckka in forum C Programming
    Replies: 5
    Last Post: 03-15-2017, 04:34 AM
  2. Replies: 5
    Last Post: 02-02-2013, 09:33 PM
  3. Replies: 2
    Last Post: 12-02-2012, 05:25 AM
  4. Read char by char into two-dimensional array pointer.
    By 'Serj Codito in forum C++ Programming
    Replies: 5
    Last Post: 09-08-2012, 09:47 AM
  5. Copy char array to char pointer
    By Suseela in forum C Programming
    Replies: 9
    Last Post: 08-06-2009, 12:49 PM

Tags for this Thread