Pointer of array's ( example: get the third word of the sentence )

A

Anouar

The point was to make a program that gives you the possibilty to enter
a sentence and when you press "enter" it should ask a number for
example when i press 4
It should take the 4th word out of the sentence and print it

example sentence "hello i am Anouar"
the space after hello should not be a problem for the program so whe i
ask for the second word it should give "i" for an answer..

this is what i've got so far

can anybody help me?
------------------------------------------------------------
------------------------------------------------------------

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

int woord, counter, pointer;
char *wijzer[1000];
char zin[1000];

int main(){
void zetinArray();
void printUit();

zetinArray();
printUit();
}


void zetinArray(){

printf("Enter a sentence:\n");
counter=0;
pointer =0;
zin[teller] = getchar();

while(zin[counter]!='\n'){


if(zin[counter]==' ' || zin[counter] == '\n'){
zin[counter] = '\0';
counter ++;
pointer++;
wijzer[pointer] = &zin[counter];


}else{
wijzer[pointer] = &zin[counter];

}
counter++;
zin[counter] = getchar();
}
}

void printUit(){
printf("%s\n",wijzer[0]);
printf("%s\n",wijzer[1]);
printf("%s\n",wijzer[2]);
printf("%s\n",wijzer[3]);
printf("Wich word do you want?\n");
scanf("%d",&woord);
printf("%s",wijzer[woord]);
}
 
D

dandelion

can anybody help me?
------------------------------------------------------------
------------------------------------------------------------

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

int woord, counter, pointer;
char *wijzer[1000];
char zin[1000];

Comment #1: Choosing *one* language as a base to name identifiers makes your
program more readable. I think no one but the dutch present understand
"wijzer" or "zetinArray".
int main(){
void zetinArray();
void printUit();

Comment #2: Prototypes inside a function are a bad idea. They are intended
to specify an interface, which currently is restricted to "main()" only. You
do not want to redeclare your prototypes in every function, do you?

They usually go in a *.h file, or at the top of your program if it's a
single source file.
zetinArray();
printUit();
}

Comment #3: It is usually handy to try and break down your assignment into
little steps:

1. Read a string (from stdin)
2 skip_space
3. while characters left
4. store_word
5 skip_space
6. write output.

etc.
void zetinArray(){

Comment #4

This function combines quite a lot of functionality. Try to design one
function having one piece of functionality. Break it into smaller bits.
printf("Enter a sentence:\n");
counter=0;
pointer =0;
zin[teller] = getchar();

while(zin[counter]!='\n'){


if(zin[counter]==' ' || zin[counter] == '\n'){
zin[counter] = '\0';
counter ++;
pointer++;
wijzer[pointer] = &zin[counter];

Comment #5: &zin[counter] is equivalent to zin + counter.
}else{
wijzer[pointer] = &zin[counter];

}
counter++;
zin[counter] = getchar();
}
}

Comment #6

At this point you *know* how many words you have. In your example this is 4,
but it may be 10, 20 or whatever. It's not that difficult to cope with that
using a simple for-loop.
void printUit(){
printf("%s\n",wijzer[0]);
printf("%s\n",wijzer[1]);
printf("%s\n",wijzer[2]);
printf("%s\n",wijzer[3]);
printf("Wich word do you want?\n");
scanf("%d",&woord);
printf("%s",wijzer[woord]);
}

HTH.

dandelion.
 
B

Barry Schwarz

The point was to make a program that gives you the possibilty to enter
a sentence and when you press "enter" it should ask a number for
example when i press 4
It should take the 4th word out of the sentence and print it

example sentence "hello i am Anouar"
the space after hello should not be a problem for the program so whe i
ask for the second word it should give "i" for an answer..

this is what i've got so far

can anybody help me?
------------------------------------------------------------
------------------------------------------------------------

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

int woord, counter, pointer;
char *wijzer[1000];
char zin[1000];

int main(){
void zetinArray();
void printUit();

zetinArray();
printUit();
}


void zetinArray(){

printf("Enter a sentence:\n");
counter=0;
pointer =0;
zin[teller] = getchar();

I assume teller here should be counter. Now you have an 'h' in
zin[0].
while(zin[counter]!='\n'){


if(zin[counter]==' ' || zin[counter] == '\n'){
zin[counter] = '\0';
counter ++;
pointer++;
wijzer[pointer] = &zin[counter];


}else{
wijzer[pointer] = &zin[counter];

On iteration 1 through loop, wijzer[0] points to the 'h'.
On iteration 2, wijzer[0] is changed to point to the 'e'. You have
lost your pointer to the start of the first word.
}
counter++;
zin[counter] = getchar();

On iteration 1, zin[1] contains 'e'.
}
}

void printUit(){
printf("%s\n",wijzer[0]);
printf("%s\n",wijzer[1]);
printf("%s\n",wijzer[2]);
printf("%s\n",wijzer[3]);
printf("Wich word do you want?\n");
scanf("%d",&woord);
printf("%s",wijzer[woord]);

The first word is pointed to by wijzer[0]. Unless you want to force
your user to count from 0, you should use wijzer[woord-1].



<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top