Thread: Program help (already written just need help)

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    7

    Program help (already written just need help)

    These are the instructions:

    Program to implement a function strFind that accepts two strings (character arrays). The first string is the string to find and the second string is the string to search. You should return the index of the first character of the string found in the search string. Although you are returning an integer index, this program is to be otherwise pointer-based. You may find it helpful to write the program first without pointers to work through the logic and then convert it to be pointer-based.

    I'm just a little confused with the pointers. if you can help me or give me example code so I can write it myself that would be great.

    here is what I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int strfind(char array1[], char array2[]) //declare function
    {
        int loopcount1 = 0;
        int loopcount2 = 0;
        int index = -1;//assume not found
        bool found = false;
        while(array2[loopcount2])
        {
            found = true;
            printf("%c \n",array1[loopcount1]);
            if(array1[loopcount1] == array2[loopcount2])
            {
                loopcount1++;
                loopcount2++;
            }
            else
            {                       
                loopcount1 = 0;
                loopcount2++;
            }
      //      if(array1[loopcount1] == '\0')
              if(!array1[loopcount1])
            {
                 index = loopcount2 - loopcount1;
                 break;                
            }//end of if      
        } // end of while  
        return index;
    } //end of function
    
    
    char array1[] = "ta";
    char array2[] = "catalog";
    
    int main()
    {
        int index;
        if((index = strfind(array1, array2)) != -1)
        {
              printf("found at %d\n",index);
        }
        system("pause");
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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. have i written this program correctly ?
    By broli86 in forum C Programming
    Replies: 6
    Last Post: 07-20-2008, 03:20 PM
  2. What's the Biggest Program You have ever Written?
    By Davros in forum A Brief History of Cprogramming.com
    Replies: 37
    Last Post: 12-04-2004, 01:40 AM
  3. matrix program written in c++
    By boa_321 in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2003, 09:33 AM
  4. Another C - Program is to be written
    By sonusden in forum C Programming
    Replies: 2
    Last Post: 02-01-2002, 10:24 AM
  5. C- Program which is to be written?
    By sonusden in forum C Programming
    Replies: 2
    Last Post: 02-01-2002, 10:00 AM