string

babadinding

Cadet 2nd Year
Registriert
Apr. 2015
Beiträge
16
hello
kann jemanden mir sagen woran liegt der ausgabe so ist:

bitte geben sie wort1 ein:hello
hello
bitte geben sie wort2 ein:world
world

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main (){

char wort1[71], wort2[11], wort3[71];
int i, x,j;

printf("bitte geben sie wort1 ein:");
fgets(wort1,71,stdin);
printf("%s", wort1);

printf("bitte geben sie wort2 ein:");
fgets(wort2,11,stdin);
printf("%s\n", wort2);

for(i=0; i<=strlen(wort1); i++){


for(x=0; x<=strlen(wort2); x++){

if(wort1 != wort2[x]){

for(j=0; j<=71; j++){
wort3[j];

}
printf("%c",wort3[j]);
}
}

}

return 0;}
 
Zuletzt bearbeitet von einem Moderator:
Was genau willst du erklärt haben? Funktioniert es so wie du willst oder soll etwas anders sein? Verstehst du C grundsätzlich? Formartier mal bitte deinen Quelltext zwecks besserer Lesbarkeit.
 
Zuletzt bearbeitet:
ich möchte zwei arrays vergleichen und gibt den einzelen zeichen die nicht in den anderen vorhanden aus zu geben

Danke

ich kann auch string compare verwenden aber mir wollte so probieren
 
Zuletzt bearbeitet von einem Moderator:
Ich verstehe auch nicht genau was du damit vorhast, jedoch würde ich behaupten das die Variabale j im printf nicht mehr verfügbar ist.
Ausserhalb des Contextes.
 
Was bezweckst du denn mit dem hier?
Code:
for(j=0; j<=71; j++){
wort3[j];

}
Da passiert ja nichts.
Hiermit
Code:
printf("%c",wort3[j]);
gibst du auch nichts sinnvolles aus (bzw. gar nichts), da j nach der Schleife 72 ist und damit außerhalb des Wertebereichs des Strings (außerdem ist wort3 leer).

Willst du im Fall, dass im ersten String etwas anderes steht als im zweiten, den Wert des ersten oder des zweiten Strings ausgeben? Theoretisch bräuchtest du für beide Fälle keinen dritten String, wenn nur eine Ausgabe erfolgen soll.

Wäre super, wenn du deinen Code erläutern könntest bzw. erklärst, was der Code theoretisch machen soll.
 
Zuletzt bearbeitet:
ok
i wanted to give two different arrays one with 16 character and the other one with 10
after that i want the program to compare the particular characters that in both arrays and give me the characters that can be found in array with 11 but not in array 71

example
array1[16], array2[10]
array1: helloworld
array2: hello

the output should be as : wrld
 
Zuletzt bearbeitet von einem Moderator:
Code:
char wort1[71]
Then you shouldn't have 71 places for the first array if you want 16 characters for the first array.
I would do it like this:
Use a break-statement that stops the procedure if wort1 == wort2[x]. If the break does not stop the procedure, you can save the character or print it out (if you checked whether it occured before).
 
Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main() {

	char wort1[100] = {0};
	char wort2[100] = {0};
	char wort3[100] = {0};

	int i = 0;
	int x = 0;
	int j = 0;
	int counter = 0;

	printf("Bitte geben Sie wort1 ein:");
	scanf("%s", wort1);

	printf("Bitte geben Sie wort2 ein:");
	scanf("%s", wort2);

	for(i = 0; wort1[i] != '\0'; i++) {

		counter = 0;

		for(x = 0; wort2[x] != '\0'; x++) {

			if(wort1[i] != wort2[x]){
				counter++;
			}
			if(counter == strlen(wort2)) {
				wort3[j] = wort1[i];
				j++;
			}
		}
	}
	wort3[j] = '\0';
	printf("%s", wort3);
	return 0;
}

Sollte das machen was du dir gewunschen hast.
 
Zuletzt bearbeitet:
Zurück
Oben