Beispiel 1: anzahlWorte. Bitte erinnern: WISO 210.

06.11.2016 10:30 Uhr
#include <iostream>

using namespace std;

int main()
{
char zeile[100] =
   "Dies ist ein Text aus mi Nacht.";
int worte[20];
int anzahlWorte;

worte[0]=0;
anzahlWorte=1;
for (int i=0; zeile[i] != '\0'; i = i + 1)
    if (zeile[i]==' ')
       {
       zeile[i]='\0';
       worte[anzahlWorte]=i+1;
       anzahlWorte=anzahlWorte+1;
       }

for (int i=0; i<anzahlWorte; i++)
    {
    cout << "Ein Wort: ";
    for (int j=worte[i]; zeile[j] != '\0'; j = j + 1)
        cout << zeile[j];
    cout << endl;
    }
}

Beispiel 1: Erläuterungen I

#include <iostream>

using namespace std;

int main()
{
char zeile[100] =
   "Dies ist ein Text aus mi Nacht.";

Eine Zeichenkette von bis zu 100 Zeichen. Jede Zeichenkette endet mit einer Null.

Im Speicher steht also:

zeile[0]                                                                              D
zeile[1]                                                                              i
zeile[2]                                                                              e
zeile[3]                                                                              s
zeile[4]                                                                              <Leerstelle>
zeile[5]                                                                              i
zeile[6]                                                                              s
zeile[7]                                                                              t
zeile[8]                                                                              <Leerstelle>
zeile[9]                                                                              e
zeile[10]                                                                              i
zeile[11]                                                                              n
zeile[12]                                                                              <Leerstelle>
zeile[13]                                                                              T
zeile[14]                                                                              e
zeile[15]                                                                              x
zeile[16]                                                                              t
zeile[17]                                                                              <Leerstelle>
zeile[18]                                                                              a
zeile[19]                                                                              u
zeile[20]                                                                              s
zeile[21]                                                                              <Leerstelle>
zeile[22]                                                                              m
zeile[23]                                                                              i
zeile[24]                                                                              <Leerstelle>
zeile[25]                                                                              N
zeile[26]                                                                              a
zeile[27]                                                                              c
zeile[28]                                                                              h
zeile[29]                                                                              t
zeile[30]                                                                              .
zeile[31]                                                                              ‚\0‘

int worte[20];
int anzahlWorte;

worte[0]=0;
anzahlWorte=1;
for (int i=0; zeile[i] != '\0'; i = i + 1)
    if (zeile[i]==' ')
       {
       zeile[i]='\0';
       worte[anzahlWorte]=i+1;
       anzahlWorte=anzahlWorte+1;
       }

Jetzt wurden in unserer Zeichenkette die Leerstellen durch Nullen ersetzt.

Im Speicher steht also:

zeile[0]                                                                              D
zeile[1]                                                                              i
zeile[2]                                                                              e
zeile[3]                                                                              s
zeile[4]                                                                              ‚\0‘
zeile[5]                                                                              i
zeile[6]                                                                              s
zeile[7]                                                                              t
zeile[8]                                                                              ‚\0‘
zeile[9]                                                                              e
zeile[10]                                                                              i
zeile[11]                                                                              n
zeile[12]                                                                              ‚\0‘
zeile[13]                                                                              T
zeile[14]                                                                              e
zeile[15]                                                                              x
zeile[16]                                                                              t
zeile[17]                                                                              ‚\0‘
zeile[18]                                                                              a
zeile[19]                                                                              u
zeile[20]                                                                              s
zeile[21]                                                                              ‚\0‘
zeile[22]                                                                              m
zeile[23]                                                                              i
zeile[24]                                                                              ‚\0‘
zeile[25]                                                                              N
zeile[26]                                                                              a
zeile[27]                                                                              c
zeile[28]                                                                              h
zeile[29]                                                                              t
zeile[30]                                                                              .
zeile[31]                                                                              ‚\0‘

Wenn eine Zeichenkette „irgendwo“ beginnt uns bis zur ersten Null geht, haben wir jetzt doch eigentlich SIEBEN Zeichenketten?

Die Situation könnten wir auch so beschreiben:

zeile[0+0]                                                                              D
zeile[0+1]                                                                              i
zeile[0+2]                                                                              e
zeile[0+3]                                                                              s
zeile[0+4]                                                                              ‚\0‘
zeile[5+0]                                                                              i
zeile[5+1]                                                                              s
zeile[5+2]                                                                              t
zeile[5+3]                                                                              ‚\0‘
zeile[9+0]                                                                              e
zeile[9+1]                                                                              i
zeile[9+2]                                                                              n
zeile[9+3]                                                                              ‚\0‘
zeile[13+0]                                                                              T
zeile[13+1]                                                                              e
zeile[13+2]                                                                              x
zeile[13+3]                                                                              t
zeile[13+4]                                                                              ‚\0‘
zeile[18+0]                                                                              a
zeile[18+1]                                                                              u
zeile[18+2]                                                                              s
zeile[18+3]                                                                              ‚\0‘
zeile[22+0]                                                                              m
zeile[22+1]                                                                              i
zeile[22+2]                                                                              ‚\0‘
zeile[25+0]                                                                              N
zeile[25+1]                                                                              a
zeile[25+2]                                                                              c
zeile[25+3]                                                                              h
zeile[25+4]                                                                              t
zeile[25+5]                                                                              .
zeile[25+6]                                                                              ‚\0‘

for (int i=0; i<anzahlWorte; i++)
    {
    cout << "Ein Wort: ";
    for (int j=worte[i]; zeile[j] != '\0'; j = j + 1)
        cout << zeile[j];
    cout << endl;
    }

In der obigen Schleife haben wir uns das zu Nutze gemacht, indem wir den Beginn eines Wortes jweils in einem Element des Arrays Worte abgespeichert haben, also:

zeile[worte[0]+0]                                                                              D
zeile[worte[0]+1]                                                                              i
zeile[worte[0]+2]                                                                              e
zeile[worte[0]+3]                                                                              s
zeile[worte[0]+4]                                                                              ‚\0‘
zeile[worte[1]+0]                                                                              i
zeile[worte[1]+1]                                                                              s
zeile[worte[1]+2]                                                                              t
zeile[worte[1]+3]                                                                              ‚\0‘
zeile[worte[2]+0]                                                                              e
zeile[worte[2]+1]                                                                              i
zeile[worte[2]+2]                                                                              n
zeile[worte[2]+3]                                                                              ‚\0‘
zeile[worte[3]+0]                                                                              T
zeile[worte[3]+1]                                                                              e
zeile[worte[3]+2]                                                                              x
zeile[worte[3]+3]                                                                              t
zeile[worte[3]+4]                                                                              ‚\0‘
zeile[worte[4]+0]                                                                              a
zeile[worte[4]+1]                                                                              u
zeile[worte[4]+2]                                                                              s
zeile[worte[4]+3]                                                                              ‚\0‘
zeile[worte[5]+0]                                                                              m
zeile[worte[5]+1]                                                                              i
zeile[worte[5]+2]                                                                              ‚\0‘
zeile[worte[6]+0]                                                                              N
zeile[worte[6]+1]                                                                              a
zeile[worte[6]+2]                                                                              c
zeile[worte[6]+3]                                                                              h
zeile[worte[6]+4]                                                                              t
zeile[worte[6]+5]                                                                              .
zeile[worte[6]+6]                                                                              ‚\0‘

 


Anhang: example1.cpp

 

 

Schlagwörter: