Saturday, 15 January 2011

c - Character Pointers and Space Searching -



c - Character Pointers and Space Searching -

i have simple lab task do, need print out characters in string twice, unless it's space.

for reason can't seem figure out, "echostring" function looping infinity.

#include <stdio.h> #include <stdlib.h> #include <ctype.h> int main(){ char* rhyme1 = "hey diddle diddle cat , fiddle"; char rhyme2[265]; strncpy (rhyme2, "the cow jumped on moon", sizeof(rhyme2)); char wordlist[8][100]; /*q1: length of string rhyme?*/ printf("length: %d", strlen(rhyme1) ); /*q2: print out each letter twice, except spaces*/ echostring(rhyme1); } void echostring ( char* pstring ) { while ( *pstring != '\0' ) { if ( !isspace( *pstring ) ) { printf("%s%s", *pstring, *pstring); } else { printf("%s", *pstring); } pstring++; } }

i feeling it's how i'm incrementing pointer, or isspace function.

thanks time.

edit: changed '/0' '\0'. sense dumb not seeing that.

\0 null-terminated character, not /0. alter '/0' '\0'.

use %c print char, not %s, string instead. alter %s %c.

c pointers char

No comments:

Post a Comment