C string

babadinding

Cadet 2nd Year
Registriert
Apr. 2015
Beiträge
16
hallo kann jemanden mir erklären warum meine ausgabe von diese programm so ist:
bitte geben sie ein text sin:heellooyou
h

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


int main(){

char text[100];
char text2[100];
int x,y,z;

printf("bitte geben sie ein text sin:");
fgets(text,100,stdin);

for(x=0; x<100; x++){
if(x==0)
{
printf("%c", text[x]);
}
else
{
for(y=0; y<100; y++){
if(text2[y]==text[x]){
z=0;
}

else{
text[x];
printf("%c",text[x]);
x++;
}

}

}
}
return 0;
}
 
Zuletzt bearbeitet:
1. Binde Code im Forum zur besseren Lesbarkeit bitte über den [code]...[/code]-Tag ein.
2. Was erhoffst du dir überhaupt von dem Programm? Der Code erscheint mir ziemlich sinnlos.
3. Beachte auch das hier. Eine gewisse Eigenleistung musst du erst mal bringen.

Also wie geasgt, wichtig ist hier sowieso erst mal eine genauere Schilderung des Sachverhaltes.
 
Abgesehen davon, dass ich nicht verstehe, was dein Programm überhaupt machen soll, kommt mir diese Zeile besonders komisch vor:
Code:
x<text[100]
Dein Array ist eh nur 100 Zeichen lang und du greifst hiermit auf das 101. Zeichen zu, also außerhalb des überhaupt definierten Array-Bereiches. Falls du die Schleife über die Länge des Strings laufen lassen möchtest, muss es
Code:
x<strlen(text)
heißen, falls die Länge des Arrays gemeint ist
Code:
x<100
 
der code sollte dupellt vorkommenden zeichen eliminieren und ich weist nicht an welche stelle ich wollte 101 zeichen zugreifen will.
 
@babadingding Are you not a native German speaker? I realise now that I have written this in your previous threads as well but please do put your code in the [code]...[/code]-tag when posting in this forum. That way we can name line numbers and better read code in general.
Also, if you're not a native German speaker then how come you're asking here? I'd guess that maybe you are enrolled in a German university and that these posts are concerned with tasks from your studies.
If so, we are happy to help (perhaps even in English) under the prerequisite that you do start to solve the problem on your own and tell us what you have tried so far.
Also I suggest you take the time for your posts here to be well written and understandable. Start with what you are trying to do rather than just posting some code on its own and asking 'why doesn't it work?'.
E.g. in this particular post you never wrote what your input was, so how on earth could we now what the program outputs?
 
thank you for the advise but i think you have not paid proper attention on the top part of the code where the input is and before the input it was written in german as : bitte geben sie ein text ein: and after that heellooyou is written then followed by the output which is h.

the reason why i wonder that the code is not working properrly as expected is
unclear to me for whatever reason i dont know.
i first declar two arrys each containing 100 elements and then request the program to printf me when my first element in the text array is equals to 0.
that is in the first loop in the second loop i expected the program not to executed in thes scope anymore as x is no longer equals to zero.
then in the else scope i said if the first element of y is equals to the second element of x then initialize the z to 0. otherwise go to the next scope and printf text of index of x and increment x. so why is it not doing what i requested. i am very knew in the c language
 
Zuletzt bearbeitet:
Are you more comfortable in English or in German? Because in both languages I have some problems understanding exactly what you mean.

Regarding the input, the way you've written it I was under the impression that you meant that the output was in fact "bitte geben sie ein text sin:heellooyou
h"

Now to move on. What compiler and IDE are you using? You might want to try to print all warnigs from the compiler. Then you should notice, for instance, that text2 is never filled with anything. There isn't really a need for it at all.
And the printf in the if statement in your first for loop is identical to
Code:
printf("%c", text[0])
- no need to put it in the loop. This is also the part where your program will print the output i.e. the first letter of your input.
But before I further look at your code I must insist that you put it in proper code-tags, at this point (without line numbers etc.) a discussion of the code is just not manageable.

And after that I would suggest you rethink your strategy for this particular problem.
I personally would simply shift the letters in the textarray one position to the left each time I found a letter two times in a row.
 
and then request the program to printf me when my first element in the text array is equals to 0.
no, you are not. You wrote
Code:
if(x==0)
{
printf("%c", text[x]);
which prints you the char at position x when x is 0 (only the case the first time the loop started). so it actually always prints you the first character of your string. I don't get the rest of your idea, e.g. z isn't doing anything. You just set it 0 but not using it at all. Neither does "text[x];".

If you want to eleminate all chars of the same type in a row you hace to write something like that:
Code:
for(x=0; x<strlen(text); x++){

    if (text[x] == text[x+1]) {
        memmove(&text[x], &text[x+1], strlen(&text[x]));
        x--;
    }
}
printf("%s", text);

Pls look up the usage of memmove with
Code:
man memmove
and get the idea of using pointers (here is a good tutorial: http://boredzo.org/pointers/).
Here's a good book about C too, but unfortunatly only in German : http://openbook.rheinwerk-verlag.de/c_von_a_bis_z/
 
Zuletzt bearbeitet: ("memmove(text + x*sizeof(char), text + (x+1)*sizeof(char), strlen(text)+1);" war nicht ganz korrekt)
Der trollt euch doch. :p Englisch ist nie und nimmer seine Muttersprache.
 
@antred Glaube ich auch nicht, aber neben Englisch und Deutsch gibt es ja noch andere Sprachen.
Und einige Inhalte sollten ja auch unabhänigig von der Sprache im Post vorhanden sein, die Sprache ist da gar nicht unbedingt der Knackpunkt. Sollte der TE allerdings beharrlich nicht auf hier gemachte Ratschläge eingehen, werde ich seine Themen in Zukunft auch meiden.
 
Zurück
Oben