int main() { Num num,square; int n,i; FILE *fp = fopen("palsquare.in","r"); #ifdef DEBUG FILE *debug = fopen("palsquare.tmp","w"); #endif fscanf(fp,"%d",&n); fclose(fp); fp = fopen("palsquare.out","w"); for(i = 0;i < LENGTH;i++) { num.num[i] = '0'; square.num[i] = '0'; } for(i = 1;i <= M;i++) { add1(&num,n); square = Square(num,n); #ifdef DEBUG PrintNum(debug,num,square); #endif if(IsPalindrom(square)) PrintNum(fp,num,square); } #ifdef DEBUG #endif fclose(fp); return 0; }
int main () { StringType source; StringType substring; StringType destination; printf("enter the string: "); gets(source); printf("enter the substring: "); gets(substring); int findIndex = IndexOfSubstring(source,substring); if (findIndex == -1) { printf("In this string there is no such substring\n"); } else { printf("The first general element: %i\n", findIndex); } if (IsPalindrom(source) == true) { printf("this string is a palindrome\n"); } else { printf("this string not a palindrome\n"); } MakePalindrom(source, destination); printf("palindrome:\t"); puts(destination); StringType string; printf("enter the string2:\n"); gets(string); strcat(source, " "); strcat(source, string); printf("after strcat - %s\n", source); int length; length = strlen(string); for (int i = 0; i < length; i++) { string[i] = tolower (string[i]); } printf("after tolower: %s\n", string); return 0; }
char* MakePalindrom(const char* const source) { char* destination = NULL; int sourceLength = StringLength(source); if (0 == sourceLength) return destination; destination = new char[2 * sourceLength]; if (IsPalindrom(source)) { StringCopy(destination, source); } else { char* const rightPalindrom = new char[2 * sourceLength]; MakePalindromAddingCharactersToTheRightSide(source, rightPalindrom); char* const revertedString = new char[2 * sourceLength]; for (int i = 0; i < sourceLength; i++) { *(revertedString + i) = *(source + sourceLength - i - 1); } *(revertedString + sourceLength) = '\0'; char* const leftPalindrom = new char[2 * sourceLength]; MakePalindromAddingCharactersToTheRightSide(revertedString, leftPalindrom); int rightPalindromLength = StringLength(rightPalindrom); int leftPalindromLength = StringLength(leftPalindrom); if (rightPalindromLength > leftPalindromLength) { StringCopy(destination, leftPalindrom); } else { StringCopy(destination, rightPalindrom); } delete[] rightPalindrom; delete[] revertedString; delete[] leftPalindrom; } return destination; }
int main () { char source[20]; char substring[20]; printf("enter the string: "); gets(source); printf("enter the substring: "); gets(substring); char* ptrSource = new char[20]; strcpy(ptrSource, source); char* ptrSubstring = new char[20]; strcpy(ptrSubstring, substring); int findIndex = IndexOfSubstring(ptrSource, ptrSubstring); if (findIndex == -1) { printf("In this string there is no such substring\n"); } else { printf("The first general element: %i\n", findIndex); } if (IsPalindrom(ptrSource) == true) { printf("this string is a palindrome\n"); } else { printf("this string not a palindrome\n"); } printf("palindrome:\t"); puts(MakePalindrom(ptrSource)); delete[] ptrSource; delete[] ptrSubstring; return 0; }
void MakePalindromAddingCharactersToTheRightSide(const char* const source, char* const destination) { int sourceLength = StringLength(source); char* const localPalindrom = new char[2 * sourceLength]; StringCopy(localPalindrom, source); for (int iterCount = 1; iterCount < sourceLength; iterCount++) { for (int charCopyCount = iterCount, i = 0; charCopyCount > 0; charCopyCount--, i++) { *(localPalindrom + sourceLength + i) = *(localPalindrom + charCopyCount - 1); } *(localPalindrom + sourceLength + iterCount) = '\0'; if (IsPalindrom(localPalindrom)) { StringCopy(destination, localPalindrom); break; } } delete[] localPalindrom; }
int main() { int errorCode = 0; do { char buffer[150]; char* string1 = NULL; char* substring1 = NULL; // Make input string for user printf("Input some string:\n"); gets_s(buffer); if (0 == buffer) { errorCode = -1; break; } // Create dynamic string int stringLength1 = strlen(buffer); string1 = new char[stringLength1 + 1]; // +1 for '\0' strcpy(string1, buffer); #ifdef SHOW_MEMORY_ERRORS #define MEMORY_ERROR 2 #if 1 == MEMORY_ERROR // Wrong creation of dynamic string // Run app by CTRL-F5 in Debug version char* tempString = new char[stringLength1]; tempString[stringLength1] = 'f'; strcpy(tempString, buffer); delete[] tempString; tempString = NULL; // If you want use this variable later, you need clear pointer #else if 2 == MEMORY_ERROR // This is extansion of previous example // Start from here debuging and watch to variables char someString1[5] = "1234"; char someString2[5] = "abcd"; strcpy(someString2, "zxcv-------------"); int sizeOfStr1 = sizeof(someString1); int sizeOfStr2 = sizeof(someString2); #endif #endif printf("Input substring for search:\n"); gets_s(buffer); if (0 == buffer) { errorCode = -2; break; } int substringLength1 = strlen(buffer); substring1 = new char[substringLength1 + 1]; // +1 for '\0' strcpy(substring1, buffer); printf("Index of first entering for substring: %i\n", IndexOfSubstring(string1, substring1)); /////////////////////////////////////////////////////////////////////////// char* string2 = NULL; printf("Input string for palindrom check:\n"); gets_s(buffer); if (0 == buffer) { errorCode = -3; break; } // Create dynamic string int stringLength2 = strlen(buffer); string2 = new char[stringLength2 + 1]; strcpy(string2, buffer); printf("\nInput string %s palindrom\n", IsPalindrom(string2) ? "is" : "isn't"); ////it's the same as: //char* choiseStr; //if (IsPalindrom(string2)) //{ // choiseStr = "is"; //} //else //{ // choiseStr = "isn't"; //} //printf("Input string %s palindrom\n", choiseStr); /////////////////////////////////////////////////////////////////////////// // Some test strings: // aaabccba // aaabcba // aa // a char* string3 = NULL; printf("Input string for making palindrom:\n"); gets_s(buffer); if (0 == buffer) { errorCode = -4; break; } // Create dynamic string int stringLength3 = strlen(buffer); string3 = new char[stringLength3 + 1]; strcpy(string3, buffer); char* palindromString = MakePalindrom(string3); printf("Maded palindrom '%s'\n", palindromString); #ifndef SIMULATE_MEMORY_LEAKS delete[] string1; delete[] substring1; delete[] string2; delete[] string3; delete[] palindromString; #else //delete[] string1; //delete[] substring1; //delete[] string2; //delete[] string3; //delete[] palindromString; //MakePalindrom("aba"); #endif } while (false); #ifdef NEED_FIND_LEAKS if (0 == errorCode && _CrtDumpMemoryLeaks()) { printf("!!! There are some leaks. See 'Output' window in Visual Stusio.\n"); getchar(); } #endif return errorCode; }
void MenuSupport::runMenu() { bool canExit = false; while (!canExit) { printMenuAndTakeChoise(); if (_userChoise == 0) continue; switch (_userChoise) { case kReturStringLenghtChoiseType: { printf(kEnterAimStringMessage); printf(kInputSymbolMessage); char* ptrAimStringLenght = new char[maxStringLenght]; gets_s(ptrAimStringLenght, maxStringLenght); int stringLenght = StringLength(ptrAimStringLenght); printf(kLenghtOfAimStringMessage, stringLenght); delete[] ptrAimStringLenght; _userChoise = 0; break; } case kCopyOneStringToAnotheChoiseType: { printf(kEnterDestinationStringMessage); printf(kInputSymbolMessage); char* ptrDestinationStringCopy = new char[maxStringLenght]; gets_s(ptrDestinationStringCopy, maxStringLenght); printf(kEnterSourceStringMessage); printf(kInputSymbolMessage); char* ptrSourceStringCopy = new char[maxStringLenght]; gets_s(ptrSourceStringCopy, maxStringLenght); StringCopy(ptrDestinationStringCopy, ptrSourceStringCopy); printf(kDestinationStringAfterCPInfoMessage, ptrDestinationStringCopy); delete[] ptrDestinationStringCopy; delete[] ptrSourceStringCopy; _userChoise = 0; break; } case kStringConcatanationChoiseType: { printf(kEnterDestinationStringConcatenationMessage); printf(kInputSymbolMessage); char* ptrDestinationStringConcatenation = new char[maxStringLenght]; gets_s(ptrDestinationStringConcatenation, maxStringLenght); printf(kEnterSourceStringConcatenationMessage); printf(kInputSymbolMessage); char* ptrSourceStringConcatenation = new char[maxStringLenght]; gets_s(ptrSourceStringConcatenation, maxStringLenght); StringConcatenation(ptrDestinationStringConcatenation, ptrSourceStringConcatenation); printf(kDestinationStringAfterConcatInfoMessage, ptrDestinationStringConcatenation); delete[] ptrDestinationStringConcatenation; delete[] ptrSourceStringConcatenation; _userChoise = 0; break; } case kSubstringIndexFindeChoiseType: { printf(kEnterSourceStringForSearchingMessage); printf(kInputSymbolMessage); char* ptrSourceStringForSearching = new char[maxStringLenght]; gets_s(ptrSourceStringForSearching, maxStringLenght); printf(kEnterSearchingSubstringMessage); printf(kInputSymbolMessage); char* ptrSearchingSubstring = new char[maxStringLenght]; gets_s(ptrSearchingSubstring, maxStringLenght); int index = IndexOfSubstring(ptrSourceStringForSearching, ptrSearchingSubstring); printf(kIndexResultInfoMessage, index+1); delete[] ptrSourceStringForSearching; delete[] ptrSearchingSubstring; _userChoise = 0; break; } case kFindOutPolindromChoiseType: { printf(kEnterStringForPolindromCheckMessage); printf(kInputSymbolMessage); char* ptrStringPolindromCheck = new char[maxStringLenght]; gets_s(ptrStringPolindromCheck, maxStringLenght); printf(kPolindromCheckMessage); if (IsPalindrom(ptrStringPolindromCheck)) { printf(kConfirmMessage); } else { printf(kDenialMessage); } delete[] ptrStringPolindromCheck; _userChoise = 0; break; } case kMakePolindromChoiseType: { printf(kEnterStringForPolindromMakeMessage); printf(kInputSymbolMessage); char* ptrStringPolindromMake = new char[maxStringLenght]; gets_s(ptrStringPolindromMake, maxStringLenght); char* ptrPolindrom = MakePalindrom(ptrStringPolindromMake); printf(kResultPolindromMessage, ptrPolindrom); delete[] ptrPolindrom; delete[] ptrStringPolindromMake; _userChoise = 0; break; } case kCleanScreanChoiseType: { system("cls"); _userChoise = 0; break; } case kExitProgramChoiseType: { printf(kExitConfirmationMessage); printf(kInputSymbolMessage); char answerString[20]; gets_s(answerString); int lenght = strlen(answerString); for (int i = 0; i < lenght; i++) answerString[i] = tolower(answerString[i]); if (strcmp(answerString, "y") == 0 || strcmp(answerString, "yes") == 0) canExit = true; break; } default: { printf(kEnterCorrectValueMessage); break; } } } }
char* MenuSupport::MakePalindrom(const char* const source) { char* result = new char[maxStringLenght]; StringCopy(result, source); if (!IsPalindrom(source)) { int sourceLenght = StringLength(source); int iteretionNumber = 0; for (iteretionNumber = 0; iteretionNumber < sourceLenght; iteretionNumber++) { char* tempStringBegin = new char[maxStringLenght]; char* tempStringEnd = new char[maxStringLenght]; int index = 0; for (index = 0; index < iteretionNumber + 1; index++) { tempStringBegin[index] = source[index]; } tempStringBegin[index] = '\0'; StringCopy(tempStringEnd, &source[iteretionNumber + 1]); if (IsPalindrom(tempStringBegin) && StringLength(tempStringBegin) > 1) { char* newEnd = new char[maxStringLenght]; StringCopy(newEnd, tempStringEnd); StringRevers(tempStringEnd); StringCopy(result, tempStringEnd); int resultLenght = StringLength(result); for (int i = 0; tempStringBegin[i]; i++) { result[resultLenght] = tempStringBegin[i]; resultLenght++; } result[resultLenght] = '\0'; StringConcatenation(result, newEnd); delete[] newEnd; delete[] tempStringBegin; delete[] tempStringEnd; break; } else if (IsPalindrom(tempStringEnd)) { StringRevers(tempStringBegin); StringConcatenation(result, tempStringBegin); delete[] tempStringBegin; delete[] tempStringEnd; break; } else delete[] tempStringBegin; delete[] tempStringEnd; continue; } } return result; }