给定一个字符串,当其中出现连续三个相同的小写字母时,将其变为这个字母的下一个字母,如果是z,则变为a,如 "aaac" 变为 “bc”。变换后,如果又出现了连续三个相同的小写字母,则继续变换。最终的输出的字符不含有连续三个相等的小写字母。
#include <stdlib.h> #include <string.h> #include <iostream> int ChangeString(char *pInStr, char *pOutStr) { if (pInStr == 0 || pOutStr == 0) return -1; bool flag = true; char input[256]; strcpy(input, pInStr); char *output = pOutStr; int len, i; while (flag) { len = (int) strlen(input); for (i = 0; i < len; ++i) { if (input[i] >= 'a' && input[i] <= 'z' && i <= len - 3) { if (input[i] == input[i + 1] && input[i] == input[i + 2]) { break; } } } if (i == len) flag = false; for (i = 0; i <= len;) { if (input[i] >= 'a' && input[i] <= 'z' && i <= len - 3) { if (input[i] == input[i + 1] && input[i] == input[i + 2]) { *output = input[i] + 1; if (*output == 'z' + 1) { *output = 'a'; } ++output; i += 3; continue; } } *output++ = input[i++]; } *output = '\0'; memset(input, 0, 256); strcpy(input, pOutStr); if (!flag) break; output = pOutStr; memset(output, 0, len); } return 0; } int main() { char *pIn = "jkds*^*(HKEEEklIdddjilzzzzzzabb"; char pTest[200]; ChangeString(pIn, pTest); std::cout<< pTest ; // jkds*^*(HKEEEklIejilc }
找出给定字符串中第一个出现次数最多的字符。
#include <map> bool FindChar(char* pInputString, char* pChar) { if(pInputString == 0 || pChar == 0) return false; std::map<char,int> m; char *p = pInputString; while(*p) m[*p++] = 0; p = pInputString; while(*p) m[*p++]++; p = pInputString; *pChar = *pInputString; while(*p) { if( m[*p] > m[*pChar ] ) *pChar = *p; ++p; } return true; }
将一个整数反序,并按照指定格式输出。如-123,输出"-1 2 3"。
#include <stdlib.h> #include <vector> int ProcessX(int iInput, int& iNum, char * strRst, int& iOutput) { if(iInput > 99999 || iInput < -99999 || strRst == 0) return -1; if(iInput == 0){ iNum = 1; *strRst++ = '0'; *strRst = '\0'; iOutput = 0; return 0; } std::vector<int> ve; bool isNegative = false; if( iInput < 0 ) { isNegative = true; iInput *= -1; } while( iInput != 0 ){ ve.push_back( iInput % 10 ); iInput /= 10; } iNum = (int)ve.size(); iOutput = 0; for(unsigned int i = 0; i < ve.size(); ++i){ iOutput = iOutput*10 + ve[i]; } if(isNegative) { iOutput *= -1; *strRst++ = '-'; } for(int i = (int)ve.size()-1; i >= 0; --i){ *strRst++ = ve[i] + '0'; *strRst++ = ' '; } *(--strRst) = '\0'; return 0; }
字符串替换,注意成功返回值。
int ProcessString( char * strInput,char chSrc,char chDes ,char * strOutput) { if(strInput == 0 || strOutput == 0) return -1; bool flag = false; while(*strInput){ if(*strInput == chSrc){ flag = true; *strOutput++ = chDes; strInput++; continue; } *strOutput++ = *strInput++; } *strOutput = '\0'; if(flag) return 0; return -1; }
给定一个升序数组,找出相加等于特定值的两个数字,任意两个即可。
bool FindTwoNumbersWithSum(int aData[], unsigned int uiLength, int sum, int *pNum1, int *pNum2) { if( aData == 0) return false; unsigned i,j; i = 0; j = uiLength-1; int cal = 0; while(i < j){ cal = aData[i] + aData[j]; if( cal > sum){ --j; } if( cal < sum){ ++i; } if ( cal == sum){ *pNum1 = aData[i]; *pNum2 = aData[j]; return true; } } return false; }
原文地址:http://blog.csdn.net/thisinnocence/article/details/42216281