在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
转自:C语言字符串操作函数 - strcpy、strcmp、strcat、反转、回文 作者:jcsu C语言字符串操作函数
{ char temp, *end = s + strlen(s) - 1; while( end > s) { temp = *s; *s = *end; *end = temp; --end; ++s; } }
{ char temp; for(char *end = s + strlen(s) - 1; end > s ; --end, ++s) { temp = *s; *s = *end; *end = temp; } }
{ for(char *end = s + strlen(s) - 1; end > s ; --end, ++s) { *s ^= *end; *end ^= *s; *s ^= *end; } }
{ for(char *end = s + strlen(s) - 1; end > s ; --end, ++s) { *s ^= *end ^= *s ^= *end; } }
void strRev(char *s)
{ for(char *end = s + strlen(s) - 1; end > s ; *s++ ^= *end ^= *s ^= *end--); }
{ if(s[0] == '\0') return; else strRev(&s[1]); printf("%c",s[0]); }
版本1 { char *p=dest; while(*dest++ = *src++) ; dest=p; }
{ char *p = dst; while( *p ++ = *src ++ ) ; return dst; }
{ int i=0; for(; *(src+i)!='\0'; i++) *(dest+i) = *(src+i); *(dest+i) = '\0'; }
版本1 - 附说明 { if( 0 == exp ) return 1; return base*power(base, exp-1); } int __cdecl atoi(const char *s) { int exp=0, n=0; const char *t = NULL; for(; *s == ' ' || *s == '\t' || *s == '\n'; s++) //找到第一个非空字符 ; if( *s >'9' || *s <'0' ) //如果第一个非空字符不是数字字符,返回0 return 0; for(t=s; *t >='0' && *t <='9'; ++t) //找到第一个非数字字符位置 - 方法1 ; t--; /* 找到第一个非数字字符位置 - 方法2 t=s; while(*t++ >='0' && *t++ <='9') ; t -= 2; */ while(t>=s) { n+=(*t - 48)*power(10, exp); //数字字符转化为整数 t--; exp++; } return n; }
{ int exp=0, n=0; const char *t = NULL; for(; *s == ' ' || *s == '\t' || *s == '\n'; s++) //略过非空字符 ; if( *s >'9' || *s <'0' ) return 0; for(t=s; *t >='0' && *t <='9'; ++t) ; t--; while(t>=s) { n+=(*t - 48)*pow(10, exp); t--; exp++; } return n; }
版本1 - while版 { int i = 0; while( *s ) { i++; s++; } return i; }
size_t __cdecl strlen(const char * s)
{ for(int i = 0; *s; i++, s++) ; return i; }
{ if(*s == '\0') return 0; else return (strlen(++s) + 1); }
size_t __cdecl strlen(const char * s)
{ return *s ? (strlen(++s) + 1) : 0; }
版本1 - while版 { char *p = dst; while( *p ) p++; while( *p ++ = *src ++ ) ; return dst; }
版本1 - 错误的strcmp { for(; *a !='\0' && *b !='\0'; a++, b++) if( *a > *b) return 1; else if ( *a==*b) return 0; else return -1; }
{ int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *src) ++src, ++dst; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); } int is_vowel(char a) { switch(a) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return 1; break; default: return 0; break; } } int count_vowel(const char *s) { int num; if(s[0] == '\0') num = 0; else { if(is_vowel(s[0])) num = 1 + count_vowel(&s[1]); else num = count_vowel(&s[1]); } return num; } int main() { char *s=" AobCd ddudIe"; printf("%d \n", count_vowel(s)); return 0; } #include <stdio.h>
* 程序功能:判断一个单词,或不含空格、标点符号的短语是否为回文(palindrome) */ #include <stdio.h> #include <ctype.h> int is_palindrome(const char *s) { bool is_palindrome=0; const char *end=s; if(*end == '\0') /* 如果s为空串,则是回文 */ is_palindrome=1; while(*end) ++end; /* end指向串s最后一个字符位置 */ --end; while(s<=end) { while(*s==' ' || !isalpha(*s)) /* 略去串s中的非字母字符 */ ++s; while(*end==' ' || !isalpha(*end)) --end; if(toupper(*s) == toupper(*end)) /* 将s中的字母字符转换为大字进行判断 */ { ++s; --end; } else { is_palindrome=0; break; } /* 在s<=end的条件下,只要出现不相等就判断s不是回文 */ } if(s>end) is_palindrome=1; else is_palindrome=0; return (is_palindrome); } int main() { const char *s ="Madam I' m Adam"; printf("%s %s \n", s, is_palindrome(s) ? "is a palindrome!": "is not a palindrome!"); return 0; }
Dogma: I am God Never odd or even Too bad – I hid a boot Rats live on no evil star No trace; not one carton Was it Eliot's toilet I saw? Murder for a jar of red rum May a moody baby doom a yam? Go hang a salami; I'm a lasagna hog! Satan, oscillate my metallic sonatas! A Toyota! Race fast... safe car: a Toyota Straw? No, too stupid a fad; I put soot on warts Are we not drawn onward, we few, drawn onward to new era? Doc Note: I dissent. A fast never prevents a fatness. I diet on cod No, it never propagates if I set a gap or prevention Anne, I vote more cars race Rome to Vienna Sums are not set as a test on Erasmus Kay, a red nude, peeped under a yak Some men interpret nine memos Campus Motto: Bottoms up, Mac Go deliver a dare, vile dog! Madam, in Eden I'm Adam Oozy rat in a sanitary zoo Ah, Satan sees Natasha Lisa Bonet ate no basil Do geese see God? God saw I was dog Dennis sinned
字符串反转
C语言字符串操作函数
{ char temp, *end = s + strlen(s) - 1; while( end > s) { temp = *s; *s = *end; *end = temp; --end; ++s; } }
{ char temp; for(char *end = s + strlen(s) - 1; end > s ; --end, ++s) { temp = *s; *s = *end; *end = temp; } }
{ for(char *end = s + strlen(s) - 1; end > s ; --end, ++s) { *s ^= *end; *end ^= *s; *s ^= *end; } }
{ for(char *end = s + strlen(s) - 1; end > s ; --end, ++s) { *s ^= *end ^= *s ^= *end; } }
void strRev(char *s)
{ for(char *end = s + strlen(s) - 1; end > s ; *s++ ^= *end ^= *s ^= *end--); }
{ if(s[0] == '\0') return; else strRev(&s[1]); printf("%c",s[0]); }
版本1 { char *p=dest; while(*dest++ = *src++) ; dest=p; }
{ char *p = dst; while( *p ++ = *src ++ ) ; return dst; }
{ int i=0; for(; *(src+i)!='\0'; i++) *(dest+i) = *(src+i); *(dest+i) = '\0'; }
版本1 - 附说明 { if( 0 == exp ) return 1; return base*power(base, exp-1); } int __cdecl atoi(const char *s) { int exp=0, n=0; const char *t = NULL; for(; *s == ' ' || *s == '\t' || *s == '\n'; s++) //找到第一个非空字符 ; if( *s >'9' || *s <'0' ) //如果第一个非空字符不是数字字符,返回0 return 0; for(t=s; *t >='0' && *t <='9'; ++t) //找到第一个非数字字符位置 - 方法1 ; t--; /* 找到第一个非数字字符位置 - 方法2 t=s; while(*t++ >='0' && *t++ <='9') ; t -= 2; */ while(t>=s) { n+=(*t - 48)*power(10, exp); //数字字符转化为整数 t--; exp++; } return n; }
{ int exp=0, n=0; const char *t = NULL; for(; *s == ' ' || *s == '\t' || *s == '\n'; s++) //略过非空字符 ; if( *s >'9' || *s <'0' ) return 0; for(t=s; *t >='0' && *t <='9'; ++t) ; t--; while(t>=s) { n+=(*t - 48)*pow(10, exp); t--; exp++; } return n; }
版本1 - while版 { int i = 0; while( *s ) { i++; s++; } return i; }
size_t __cdecl strlen(const char * s)
{ for(int i = 0; *s; i++, s++) ; return i; }
{ if(*s == '\0') return 0; else return (strlen(++s) + 1); }
size_t __cdecl strlen(const char * s)
{ return *s ? (strlen(++s) + 1) : 0; }
版本1 - while版 { char *p = dst; while( *p ) p++; while( *p ++ = *src ++ ) ; return dst; }
版本1 - 错误的strcmp { for(; *a !='\0' && *b !='\0'; a++, b++) if( *a > *b) return 1; else if ( *a==*b) return 0; else return -1; }
{ int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *src) ++src, ++dst; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); } int is_vowel(char a) { switch(a) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': return 1; break; default: return 0; break; } } int count_vowel(const char *s) { int num; if(s[0] == '\0') num = 0; else { if(is_vowel(s[0])) num = 1 + count_vowel(&s[1]); else num = count_vowel(&s[1]); } return num; } int main() { char *s=" AobCd ddudIe"; printf("%d \n", count_vowel(s)); return 0; }
* 程序功能:判断一个单词,或不含空格、标点符号的短语是否为回文(palindrome) */ #include <stdio.h> #include <ctype.h> int is_palindrome(const char *s) { bool is_palindrome=0; const char *end=s; if(*end == '\0') /* 如果s为空串,则是回文 */ is_palindrome=1; while(*end) ++end; /* end指向串s最后一个字符位置 */ --end; while(s<=end) { while(*s==' ' || !isalpha(*s)) /* 略去串s中的非字母字符 */ ++s; while(*end==' ' || !isalpha(*end)) --end; if(toupper(*s) == toupper(*end)) /* 将s中的字母字符转换为大字进行判断 */ { ++s; --end; } else { is_palindrome=0; break; } /* 在s<=end的条件下,只要出现不相等就判断s不是回文 */ } if(s>end) is_palindrome=1; else is_palindrome=0; return (is_palindrome); } int main() { const char *s ="Madam I' m Adam"; printf("%s %s \n", s, is_palindrome(s) ? "is a palindrome!": "is not a palindrome!"); return 0; }
Dogma: I am God Never odd or even Too bad – I hid a boot Rats live on no evil star No trace; not one carton Was it Eliot's toilet I saw? Murder for a jar of red rum May a moody baby doom a yam? Go hang a salami; I'm a lasagna hog! Satan, oscillate my metallic sonatas! A Toyota! Race fast... safe car: a Toyota Straw? No, too stupid a fad; I put soot on warts Are we not drawn onward, we few, drawn onward to new era? Doc Note: I dissent. A fast never prevents a fatness. I diet on cod No, it never propagates if I set a gap or prevention Anne, I vote more cars race Rome to Vienna Sums are not set as a test on Erasmus Kay, a red nude, peeped under a yak Some men interpret nine memos Campus Motto: Bottoms up, Mac Go deliver a dare, vile dog! Madam, in Eden I'm Adam Oozy rat in a sanitary zoo Ah, Satan sees Natasha Lisa Bonet ate no basil Do geese see God? God saw I was dog Dennis sinned
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论