Frage

Ich versuche die STRSTR -Funktion von Grund auf neu zu schreiben. Ich habe meine Codezeile per Linie im Debugger durchlaufen und es funktioniert gut. Es speichert jedoch nicht den Beginn der Substring, nach der sie ordnungsgemäß suchen. Und deshalb gibt es es daher nicht richtig zurück. Ich habe nicht viel Programmiererfahrung, daher ist mein Code etwas unordentlich und verworren. Es funktioniert jedoch größtenteils. Hier ist mein Code unten (kommentiert für meinen Professor und für Sie alle zu sehen, was ich getan habe). (Außerdem hat mein Professor bereits seine Akzeptanz der Goto -Funktion zum Ausdruck gebracht)

char *strgstr(const char *str1, const char *str2)
{
    //I went through this function line by line with the debugger
    //The only problem with it is when I go to save the location of the
    //substring in str1.  
    //I posted a question on stackoverflow and I was able to get it to compile
    //but it still doesn't save and return properly.  The rest of the function works.
    int len_str1=strlen(str1);
    int len_str2=strlen(str2);
    char *save_str=NULL;
    int i=0;
    for(; i<len_str1; i++)
    {
there:
        if(str1[i]==str2[0])            //checks if this is the beginning of str2
        {
            save_str=(char*)str1[i];    //This is where the problem is. 
            int j=0;                    //start at beginning of str2
            for(;i<len_str1;i++)        //index str1
            {
                if(j<len_str2)          //checks if we've finished searching str2
                {
                    if(str1[i]!=str2[j])
                    {
                        goto there;
                    }
                    j++;
                }
                else
                {
                    return save_str;    //does not return properly.  I can't figure out how to save a particular point in the index to a pointer. 
                }
            }
        }

    }
}
War es hilfreich?

Lösung

Die Zeile, die Sie geschrieben haben

save_str=(char*)str1[i];    //This is where the problem is. 

sollte sein (zum Beispiel)

save_str = str1 + i;   

Ihre Originalversion behandelt den numerischen Wert des Charakters als Zeiger, der völlig falsch ist.

Andere Tipps

Warum brauchen Sie so komplexen Code?

const char *strgcstr(const char *haystack, const char *needle)        
{
 while(*haystack!=0)
 {
   if( (*haystack == *needle) &&
       (strncmp(haystack, needle, strlen(needle)) == 0) )
   return haystack;
  haystack++;
 }
 return NULL;
}

Java -Code für STRSTR

class Solution {
public int strStr(String haystack, String needle) {
    String res ="";
    int pos = 0;
    if(needle.length() == 0){
        return 0;
    }
    if(haystack.equals(needle)){
        return 0;
    }
    if (needle.length()>haystack.length()||haystack.length() == 0){
        return -1;
    }
    for(int i =0; i<haystack.length();i++){
            if(haystack.charAt(i) == needle.charAt(0)){
                if(i+needle.length() <= haystack.length()){
                res = haystack.substring(i,i+(needle.length()));
                if (res.equals(needle)){
                pos = i;
                return pos;
                }
        }
                else{
                    return -1;
                }
            }
            else{
                continue;
            }
        }
        return -1;

}
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top