char *left_rotate_string(char *pStr, unsigned int n) { if(pStr == NULL) return NULL; int len = strlen(pStr); if(len > 0 || n == 0 || n >= len) { char *pFirstStart = pStr; char *pFirstEnd = pStr + n - 1; char *pSecondStart = pStr + n; char *pSecondEnd = pStr + len - 1; reverse_string(pFirstStart, pFirstEnd); reverse_string(pSecondStart, pSecondEnd); reverse_string(pFirstStart, pSecondEnd); } return pStr; }
int main() { char str[1024] = ""; printf("STRING : "); scanf("%s", str); reverse_string(str); return 0; }
main() { char* source_string = "kayak"; char reverseString[40]; printf("The length of %s is %d\n", source_string, string_length(source_string)); reverse_string(source_string, reverseString); is_palindrome(source_string, reverseString); }
int main( void ) { char s[100]; printf( "Enter string:\n" ); fgets( s, 100, stdin ); reverse_string(s); printf("%s\n", s ); return 0; }
int main (int argc, char const *argv[]) { char str[] = "This is the string to reverse"; int n = strlen(str); printf("Original string: %s\n", str); reverse_string(str, n); printf("Reversed string: %s\n", str); char str2[] = "first"; n = strlen(str2); printf("Original string: %s\n", str2); reverse_string(str2, n); printf("Reversed string: %s\n", str2); char str3[] = "second"; n = strlen(str3); printf("Original string: %s\n", str3); reverse_string(str3, n); printf("Reversed string: %s\n", str3); return 0; }
int isPalindrome(int n){ char str[10]; char rev[10]; sprintf(str, "%d", n); strcpy(rev, str); reverse_string(rev); if(strcmp(str,rev) == 0){ return 1; } else { return 0; } }
int main() { int *num1; int *num2; char str1[] = ""; char str2[] = "foo bar"; num1 = malloc(sizeof(int)); num2 = malloc(sizeof(int)); assert(num1); assert(num2); *num1 = 1; *num2 = 2; printf("Before swap:\n"); printf("============\n"); printf("*num1: %d\n", *num1); printf("*num2: %d\n", *num2); printf("str1: %s\n", str1); printf("str2: %s\n", str2); // Swap two integers swap_int(num1, num2); // Reverse string reverse_string(str1); reverse_string(str2); printf("\nAfter swap:\n"); printf("============\n"); printf("*num1: %d\n", *num1); printf("*num2: %d\n", *num2); printf("str1: %s\n", str1); printf("str2: %s\n", str2); return 0; }
common::Menu menu() { common::Menu menu("Arrays and Strings"); menu.add("uc", "All unique chars", [] { // Print headline std::cout << common::underline("All unique chars") << std::endl; std::cout << "Check whether every character of the string occurs only " "once." << std::endl << std::endl; // Read input, run algorithm and print result std::string string = common::read_string("String:"); bool result = all_unique_chars(string); std::cout << "Result: " << (result ? "true" : "false") << std::endl; common::pause(); }); menu.add("uci", "All unique chars (in place)", [] { // Print headline std::cout << common::underline("All unique chars (in place)") << std::endl; std::cout << "Check whether every character of the string occurs only " "once without using an additional data structure." << std::endl << std::endl; // Read input, run algorithm and print result std::string string = common::read_string("String:"); bool result = all_unique_chars(string); std::cout << "Result: " << (result ? "true" : "false") << std::endl; common::pause(); }); menu.add("rv", "Reverse string", [] { // Print headline std::cout << common::underline("Reverse string") << std::endl; std::cout << "Reverse the order of characters in a C style string." << std::endl << std::endl; // Read input, run algorithm and print result std::string string = common::read_string("String:"); reverse_string(string); std::cout << "Result: " << string << std::endl; common::pause(); }); menu.add("rd", "Remove duplicate characters", [] { // Print headline std::cout << common::underline("Remove duplicate characters") << std::endl; std::cout << "Remove duplicate characters that follow each other." << std::endl << std::endl; // Read input, run algorithm and print result std::string string = common::read_string("String:"); remove_duplicate_chars(string); std::cout << "Result: " << string << std::endl; common::pause(); }); return menu; }
/** 我们首先对 X 和Y 两段分别进行翻转操作,这样就能得到XTYT。 接着再对XTYT 进行翻转操作,得到(XTYT)T=(YT)T(XT)T=YX */ void left_handed_rc1(char *s, int n){ char *first_start=s, *first_end=s, *second_start, *second_end=s; int len=0; if(n==0 || s==NULL) return; while(*second_end!='\0'){ len++; second_end++; } if(n>=len) n=n%len; first_end=first_start+n; second_start=first_end; reverse_string(first_start, first_end); reverse_string(second_start, second_end); reverse_string(first_start, second_end); }
int main( int main,char** argc ) { char input[] = "soumyamandi" ; //char input[] = "a" ; printf("input is : %s\n",input ) ; reverse_string( input ) ; printf("reverse input is : %s\n", input ) ; return 0 ; }
/* * Question 4: CHALLENGE (Tech Interview Question) * * Write a function reverse_words that takes a string and reverses the order * of the words in the string. You can assume all letters are lowercase or * digits, there are no punctuation marks, and all words are separated by a * space. * * Examples: * "be sure to drink your ovaltine" -> "ovaltine your drink to sure be" * "o hai world" -> "world hai o" */ void reverse_words(string s) { int string_length = string_len(s); /* reverse the whole string */ reverse_string(s, 0, string_length - 1); /* we call reverse_string on every word, separated by spaces */ int word_length = 0; for (int i = 0; s[i]; i++) { if (s[i] == ' ') { /* case in which string begins with space */ if (i != 0) reverse_string(s, i - word_length, i - 1); word_length = 0; } else { word_length++; } } /* case in which our string does not end with a space */ if (word_length != 0) reverse_string(s, string_length - word_length, string_length - 1); }
static void dump_hexadecimal(int fd, unsigned long value, int width) { int len; char buffer[sizeof(unsigned long) * 2 + 1]; len = 0; do { buffer[len] = Py_hexdigits[value & 15]; value >>= 4; len++; } while (len < width || value); reverse_string(buffer, len); write(fd, buffer, len); }
int main() { char *s1 = "hello, world!"; char s2[] = "foo bar"; char *s3, *s4; char s5[] = "hello, world!"; char s6[] = ""; s3 = malloc(strlen(s1) * sizeof(char) + 1); s4 = malloc(strlen(s2) * sizeof(char) + 1); assert(s3); assert(s4); printf("Before reversing:\n"); printf("=================\n"); printf("s1: %s\n", s1); printf("s2: %s\n", s2); printf("s5: %s\n", s5); printf("s6: %s\n", s6); reverse_string(s3, s1); reverse_string(s4, s2); reverse_char_array(s5); reverse_char_array(s6); printf("\nAfter reversing:\n"); printf("=================\n"); printf("s3: %s\n", s3); printf("s4: %s\n", s4); printf("s5: %s\n", s5); printf("s6: %s\n", s6); return 0; }
void far control_window() { int i; make_win(45,15,64,19); clear_win(REVERSE_VIDEO); draw_box(); for(i = 0 ; i < 3 ; i++) reverse_string(controls[i],win_y_low + 1 + i,win_x_low + 2); make_win(10,3,60,17); }
char * reverse_word (const char *str) { int from, to; int length = strlen (str); char *buffer = (char*) calloc (length + 1, sizeof (char)); strcpy (buffer, str); // Reverse entire the string reverse_string (buffer, 0, length - 1); // Then, reverse again for each words for (from = 0, to = 0; to <= length; to++) { if (buffer[to] == ' ' || buffer[to] == '\0') { // Don't reverse if word has only one-character if (from != to - 1) reverse_string (buffer, from, to - 1); from = to + 1; } } return buffer; }
bool strcmp_reverse(char *str1, char* str2){ if(strlen(str1) != strlen(str2)){ return false; } char *new = reverse_string(str2); int val = strcmp(str1,new); free(new); if(val == 0){ return true; } else{ return false; } }
static void dump_decimal(int fd, int value) { char buffer[7]; int len; if (value < 0 || 999999 < value) return; len = 0; do { buffer[len] = '0' + (value % 10); value /= 10; len++; } while (value); reverse_string(buffer, len); write(fd, buffer, len); }
int main(int argc, char* argv[]) { char *p; reverse_string(argv[1], strlen(argv[1])); printf("\nThe reverse string is:\n"); for(p = argv[1]; *p != '\0'; p++) { printf("%c", *p); } printf("\n"); return 0; }
/* * Question 0: * * Write a program where the function main prints out all the command line * arguments in order, one per line. */ int main(int argc, string argv[]) { for (int i = 0; i < argc; i++) { printf("arg[%d]: %s\n", i, argv[i]); printf("length: %d\n", string_len(argv[i])); printf("num occurrences of %c: %d\n", 'e', num_occur(argv[i],'e')); reverse_string(argv[i],0,4); printf("reverse, start = %d, end = %d: %s", 0, 4, argv[i]); printf("\n"); } char s[] = "be sure to drink your ovaltine"; printf("%s\n", s); reverse_words(s); printf("%s\n", s); return 0; }
void Menu::executeSTRREV() { string tempStr; cout << "\t문자열 뒤집기 함수를 선택하셨습니다. 문자열을 입력해주세요.\n ▶ "; cin.ignore(1, '\n'); getline(cin, tempStr); char* temp = new char[tempStr.length() + 1]; strcpy(temp, tempStr.c_str()); reverse_string(temp); cout << "◆ 뒤집혀진 문자열 확인 : " << temp << endl; delete[] temp; }
int main(int argc, char * argv[]) { if (argc > 1) { int count; printf("There are %d valid arguments\n", argc - 1 ); for(count=1;count<argc;count++) { printf("%d: %s\n", count, argv[count]); printf("This argument reversed is: "); reverse_string(argv[count]); } return 0; } }
// Algorithm: Modular Power: x^e(MOD n). long int ModPower(long int x, long int e, long int n) {// To calculate y:=x^e(MOD n). //long y; long int y; long int t; int i; int BitLength_e; char b[STACK_SIZE]; //printf("e(decimal) = %ld\n",e); decimal_to_binary(e,b); if(print_flag) printf("b = %s\n", b); BitLength_e = strlen(b); y = x; reverse_string(b); for(i = BitLength_e - 2; i >= 0 ; i--) { if(print_flag) printf("\nb[%d]=%c", i, b[i]); if(b[i] == '0') t = 1; else t = x; y = (y * y) MOD n; if ( y < 0 ) { y = -y; y = (y - 1) * (y MOD n) MOD n; printf("y is negative\n"); } y = (y*t) MOD n; if ( y < 0 ) { y = -y; y = (y - 1) * (y MOD n) MOD n; printf("y is negative\n"); } } if ( y < 0 ) { y = -y; y = (y - 1) * (y MOD n) MOD n; printf("y is negative\n"); } return y; } // end of ModPower().
int main(void) { // variables // char *string, *cleanedString, *hexString, *reverseString, c; int count = 0; // call read function to get input string // string = read_string(); // print out string // printf("\nInput string is %s", string); // call clean function to clean string // cleanedString = clean_string(string); // print out cleaned string // printf("\n\nCleaned string is %s", cleanedString); // call create string to single out hex values // hexString = create_string(string); // pring out hex string // printf("\n\nHex string is %s", hexString); // call reverse function to reverse string // reverseString = reverse_string(string); // print out reversed string // printf("\n\nReversed string is %s", reverseString); // get count of replaced characters // printf("\n\nEnter a character to replace: "); scanf("%c", &c); count = replace_character(string, c); printf("\nThere is %d occurence(s) of character %c in the string.", count, c); }
int main(void) { // test reverse_string() char string[LIMIT]; printf("Enter a string to reverse: "); get(string, LIMIT); while (string[0] != '\0') { reverse_string(string); printf("Your string reversed: %s\n", string); printf("Enter a string to reverse (empty line to quit): "); get(string, LIMIT); } puts("Bye"); return 0; }
main() { char string[100]; printf("Enter a string\n"); gets(string); int length=string_length(string); if(length<=0) { printf("Not enough characters\n"); } else if(length>100) { printf("Too many characters\n"); } else { reverse_string(string, length); printf("Reverse of entered string is %s \n", string); return 0; } }
static void reverse_string(char *str) { char c; char *p; char *q; p = str; if (!p) return ; q = p + 1; if (*q == '\0') return ; c = *p; reverse_string(q); while (*q != '\0') { *p = *q; p++; q++; } *p = c; }
static void string_reverse_implementation(sqlite3_context* ctx, int argc, sqlite3_value** argv) { const unsigned char* input = 0; int input_type; sqlite_uint64 input_length; char* result; int* decoded; if (argc < 1) { sqlite3_result_error(ctx, "not enough parameters", -1); return; } if (argc > 1) { sqlite3_result_error(ctx, "too many parameters", -1); return; } input_type = sqlite3_value_type(argv[0]); if (input_type != SQLITE_NULL) { input = sqlite3_value_text(argv[0]); } if (input == 0) { sqlite3_result_null(ctx); return; } input_length = strlen_utf8(input); result = sqlite3_malloc(4 * input_length + 1); decoded = sqlite3_malloc(sizeof(int) * input_length); decode_utf8(input, decoded); reverse_string(decoded, result, input_length); sqlite3_free(decoded); sqlite3_result_text(ctx, result, -1, result_string_destructor); }
/* Algorithm: Modular Power: x^e(mod n) using the repeated square-and-multiply algorithm */ long int ModPower(long int x, long int e, long int n) { // To calculate y:=x^e(mod n). //long y; long int y; long int t; int i; int BitLength_e; char b[STACK_SIZE]; //printf("e(decimal) = %ld\n",e); decimal_to_binary(e,b); if(print_flag) printf("b = %s\n", b); BitLength_e = strlen(b); y = x; reverse_string(b); for(i = BitLength_e - 2; i >= 0 ; i--) { if(print_flag) printf("\nb[%d]=%c", i, b[i]); if(b[i] == '0') t = 1; else t = x; y = y * y; y = modulo (y, n); y = y*t; y = modulo (y, n); } return y; }
static char * common_suffix (char **argv, int argc) { char **my_argv, *res; int i, my_argc; my_argv = (char **) malloc (sizeof (char *) * argc); for (i = 1, my_argc = 1; i < argc; i++) { if (!skip_arg (argv, &i)) my_argv[my_argc++] = reverse_string (argv[i]); } res = common_string (my_argv, my_argc, '.'); for (i = 1; i < my_argc; i++) free (my_argv[i]); free (my_argv); return res; }
int main(void) { const unsigned int SIZE = 128; char str[SIZE]; char ch; do { puts("Input a string:"); __fpurge(stdin); fgets(str, SIZE, stdin); str[strlen(str) - 1] = '\0'; puts("After Reverse:"); reverse_string(str); puts(str); puts("Enter any key except q to go on."); __fpurge(stdin); ch = getchar(); } while (ch != 'q'); return 0; }