int main() { char *str = "string"; char *wrd = "word"; char src[] = "source"; char dest[] = "destination"; char concat[ strlength(dest) + strlength(src) ]; char empty[100]; char a[] = "abc"; char b[] = "ab"; int i = 1; printf( "Testing strlength with string: %s\n", str ); printf( "String length: %d \n", strlength( str ) ); printf( "Testing strlength with string: %s\n", wrd ); printf( "String length: %d\n", strlength( wrd ) ); printf( "\n" ); printf( "Testing strcopy with string: %s\n", src ); printf( "Testing strcopy with string: %s\n", empty ); strcopy( empty, src ); printf("Empty string is now: %s\n", empty ); printf( "\n" ); printf( "Testing strconcat with string: %s\n", src ); printf( "Testing strconcat with string: %s\n", dest ); printf( "Appends the first: %d character(s) \n", i ); strconcatn( concat, dest, src, i ); printf( "Concatenated string: %s\n", concat ); printf( "\n" ); printf( "Testing strcomp with string: abc and ab \n" ); printf( "%d\n", strcomp( a, b ) ); printf( "Testing strcomp with string: abc and abc \n" ); printf( "%d\n", strcomp( a, a ) ); printf( "This answer is wrong! Still working on it." ); printf( "\n" ); printf( "Testing strchar with string: %s. Return place if found -1 if not found\n", src ); printf( "Looking for s \n" ); printf( "%d\n", strchar( src, 's' ) ); printf( "Looking for 1 \n" ); printf( "%d\n", strchar( src, '1' ) ); printf( "Looking for e \n" ); printf( "%d\n", strchar( src, 'e' ) ); printf( "\n" ); printf( "Testing strstring with strings %s and %s. Return 1 if found -1 if not found \n", a, b ); printf( "%d This answer is wrong! I used my strcomp (broken method for now) in this method which is why it churned out the wrong answer. \n", strstring( a, b ) ); return 0; }
int strindex(char *s,char *p) { char *u,*v,*w; //when the pattern string is NULL if(*s=='\0') return 0; //一旦有在s中出现和p中的第一个字符相同的就进入继续匹配 for(u=s;(u=strchar(u,*p))!=NULL;u++) { for(v=u,w=p; ;) //但到最后一个字符时即匹配成功 if(*++w=='\0') return (int)(u-s); //当发现有下一个字符不匹配就跳出循环 else if(*++v !=*w) break; } //when the source don't contain the pattern string return -1; }
void dinkc_console_process_key(SDL_KeyboardEvent kev) { if (kev.keysym.sym == SDLK_UP) { cur_line--; /* Using (a+N)%N instead of a%N to avoid negative results */ cur_line = (cur_line + NB_LINES) % NB_LINES; } else if(kev.keysym.sym == SDLK_DOWN) { cur_line++; cur_line %= NB_LINES; } else if (kev.keysym.sym == SDLK_BACKSPACE) { /* Delete last char */ int len = strlen(history[cur_line]); if (len > 0) history[cur_line][len-1] = '\0'; } else if (kev.keysym.sym == SDLK_ESCAPE) { console_active = 0; } else if (kev.keysym.unicode == SDLK_RETURN) { /* Try to parse the string */ console_return_value = dinkc_execute_one_liner(history[cur_line]); /* Go the next line */ cur_line++; cur_line %= NB_LINES; int len = strlen(history[cur_line]); if (len > 0) history[cur_line][0] = '\0'; } else if (kev.keysym.unicode != 0) { /* Append character to the current line */ if (strlen(history[cur_line]) < MAX_LINE_LEN) strchar(history[cur_line], kev.keysym.unicode); } }
int comp_num(double x,double y,char *optr) { char *str=(char*)malloc(strlen(optr)+1); strcpy(str,optr); str=strchar(str); str=strtrim(str); //printf("174:%f %f %s\n",x,y,optr); if(!strcmp(str,"==")||!strcmp(str,"=")) return !(x-y); else if(!strcmp(str,">=")) return x>=y; else if(!strcmp(str,"<=")) return x<=y; else if(!strcmp(str,">")) { //printf("182;%f %f %s result:%d\n",x,y,optr,x>y); return x>y; } else if(!strcmp(str,"<")) return x<y; else return x!=y;//for !=,~= }
int portable_snprintf(char *str, size_t str_m, const char *fmt, /*args*/ ...) { #else int portable_vsnprintf(char *str, size_t str_m, const char *fmt, va_list ap) { #endif #if defined(NEED_SNPRINTF_ONLY) va_list ap; #endif size_t str_l = 0; const char *p = fmt; /* In contrast with POSIX, the ISO C99 now says * that str can be NULL and str_m can be 0. * This is more useful than the old: if (str_m < 1) return -1; */ #if defined(NEED_SNPRINTF_ONLY) va_start(ap, fmt); #endif if (!p) p = ""; while (*p) { if (*p != '%') { /* if (str_l < str_m) str[str_l++] = *p++; -- this would be sufficient */ /* but the following code achieves better performance for cases * where format string is long and contains few conversions */ const char *q = strchar(p+1,'%'); size_t n = !q ? strlen(p) : (q-p); if (str_l < str_m) { size_t avail = str_m-str_l; fast_memcpy(str+str_l, p, (n>avail?avail:n)); } p += n; str_l += n; } else { const char *starting_p; size_t min_field_width = 0, precision = 0; int zero_padding = 0, precision_specified = 0, justify_left = 0; int alternate_form = 0, force_sign = 0; int space_for_positive = 1; /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored. */ char length_modifier = '\0'; /* allowed values: \0, h, l, L */ char tmp[32];/* temporary buffer for simple numeric->string conversion */ const char *str_arg; /* string address in case of string argument */ size_t str_arg_l; /* natural field width of arg without padding and sign */ unsigned char uchar_arg; /* unsigned char argument value - only defined for c conversion. N.B. standard explicitly states the char argument for the c conversion is unsigned */ size_t number_of_zeros_to_pad = 0; /* number of zeros to be inserted for numeric conversions as required by the precision or minimal field width */ size_t zero_padding_insertion_ind = 0; /* index into tmp where zero padding is to be inserted */ char fmt_spec = '\0'; /* current conversion specifier character */ str_arg = credits;/* just to make compiler happy (defined but not used)*/ str_arg = NULL; starting_p = p; p++; /* skip '%' */ /* parse flags */ while (*p == '0' || *p == '-' || *p == '+' || *p == ' ' || *p == '#' || *p == '\'') { switch (*p) { case '0': zero_padding = 1; break; case '-': justify_left = 1; break; case '+': force_sign = 1; space_for_positive = 0; break; case ' ': force_sign = 1; /* If both the ' ' and '+' flags appear, the ' ' flag should be ignored */ #ifdef PERL_COMPATIBLE /* ... but in Perl the last of ' ' and '+' applies */ space_for_positive = 1; #endif break; case '#': alternate_form = 1; break; case '\'': break; } p++; } /* If the '0' and '-' flags both appear, the '0' flag should be ignored. */ /* parse field width */ if (*p == '*') { int j; p++; j = va_arg(ap, int); if (j >= 0) min_field_width = j; else { min_field_width = -j; justify_left = 1; } } else if (isdigit((int)(*p))) { /* size_t could be wider than unsigned int; make sure we treat argument like common implementations do */ unsigned int uj = *p++ - '0'; while (isdigit((int)(*p))) uj = 10*uj + (unsigned int)(*p++ - '0'); min_field_width = uj; } /* parse precision */ if (*p == '.') { p++; precision_specified = 1; if (*p == '*') { int j = va_arg(ap, int); p++; if (j >= 0) precision = j; else { precision_specified = 0; precision = 0; /* NOTE: * Solaris 2.6 man page claims that in this case the precision * should be set to 0. Digital Unix 4.0, HPUX 10 and BSD man page * claim that this case should be treated as unspecified precision, * which is what we do here. */ } } else if (isdigit((int)(*p))) {