Ich versuche, diese Ausgabe zu haben:C Programmkopie String ohne strcpy() zu verwenden mit genügend Speicher
Comparing results of concat and strcat ...
strcmp("Plain old stringTroy", "Plain old stringTroy") says: 0
Die strcmp 0 zurück, wenn die beiden String-Argumente identisch sind. Wenn das Ergebnis 0 ist, verhält sich die Concat genau wie die Bibliotheksfunktion strcat.
das ist, was ich für concat Methode habe.
#define MAXSIZE 32
void concat(char dest[], char src[])
{
int i=length(src);
int j=0;
for(j; j<src[j] !='\0'; j++) {
dest[i+j] = src[j];
}
dest[i+j] = '\0';
}
Länge Methode ist:
int length(char str[])
{
// Add code here to return the length of the
// string str without using the strlen function
// Do not count the null character '\0'
// in computing the length of the string
int len=0;
int i;
for(i=0;i<str[i];i++) {
len++;
}
return len;
}
Das ist mein Haupt
int main()
{
// Variable declarations for all parts
char str2[] = "Troy";
char str4[] = "Plain old string";
char str6[MAXSIZE];
// Part 6
printf("\n----- Part 6 -----\n");
// Make a copy of the destination string first, to be reused later
strcpy(str6, str4);
concat(str4, str2);
strcat(str6, str2);
printf("Comparing results of concat and strcat ...\n");
printf("strcmp(\"%s\", \"%s\") says: %d\n",
str4, str6, strcmp(str4, str6)
);
return 0;
}
Das ist mein Ausgang ist, wenn ich es laufen:
----- Part 6 -----
Comparing results of concat and strcat ...
strcmp("PlaiTroy", "Plain old stringTroy") says: -1
Der erste String ist nicht das gleiche wie die zweite Saite, weshalb ich es bekomme Ting eine -1. Mein Problem ist in meiner Concat-Methode, aber ich kann nicht verstehen, warum es nicht gut ausgeführt wird. Liegt es an den Räumen? Wird 0 und '\ 0' nicht gut ausgeführt?
'j
auch in deiner Methode' i
Haben Sie versucht, die Zeichenfolgen zu drucken, um zu sehen, wie sie aussehen? – klutt