pointers - How to store array in address C -
basically provided main function:
#include <stdio.h> #include <string.h> #include <strings.h> #define storage 255 { int c; char s[storage]; for(;;) { (void) printf("n=%d, s=[%s]\n", c = getword(s), s); if (c == -1)break; }} we not change that
and have create function getword() should contain loop reads characters,
store in array in address provided 1 one , should stop whitespace (tab, space, eof)
basically have this:
int getword(char *w) { char str[255]; int = 0; int charcount = 0; printf("enter sentence:\n"); gets(str); /*this provided professor, i'm not sure how use while (( iochar - getchar()) !=eof); */ for(i = 0; str[i] != '\0'; i++) //loop checks tab , spaces { if(str[i] != ' ' && str[i] != '\t') { charcount++; } } printf("your string: '%s' contains %d of letters\n", str, charcount); return 0;} right output of program is:
enter sentence: hey stockoverflow string: 'hey stockoverflow' contains 16 of letters n=0, s=[] so i'm saving string , counting it, i'm not storing should be.
it should display n=16, s=[hey stockoverflow]
actually should display n=3, s=[hey]
any appreciated
this may looking for:
for(i = 0; str[i] != '\0'; i++) //loop checks tab , spaces { if(str[i] != ' ' && str[i] != '\t') { charcount++; } else { str[i] = '\0'; // terminate str break; // break out of for-loop } } printf("your string: '%s' contains %d of letters\n", str, charcount); strcpy(w, str); // add line copy str w (which s main) return strlen(w); // return length of result string
Comments
Post a Comment