void reverseWords2(char str[]) { int start = 0, end = 0, length; length = strlen(str); // reverse entire string reverseString(str, 0, length - 1); while(end < length) { if(str[end] != ' ') // skip the non-word characters { // save the position of begining of word start = end; // scan to next non-word characters while(end < length && str[end] != ' ') end++; // back up to end of word end--; // reverse word reverseString(str, start, end); } end++; // advance to next token } }
//三步翻转法 void solutionTwo(char *str, int stringLen, int rotatingNum) { rotatingNum %= stringLen; reverseString(str, 0, rotatingNum - 1); reverseString(str, rotatingNum, stringLen - 1); reverseString(str, 0, stringLen - 1); }
void trie(FILE *pool,FILE *check,FILE *result) { TRIE *head = trie_create(); char line[BUFFERSIZE]; int exitflag=0; int i; while(fgets(line,BUFFERSIZE,pool)) { /*delete the useless character '\r'*/ exitflag = trimString(line); if(!exitflag) { reverseString(line); trie_add(&head,line); } else { /*printf("Error email %s",line);*/ continue; } } while(fgets(line,BUFFERSIZE,check)) { i = 0; while(line[i]!='\r' && line[i]!='\n') i++; line[i] = '\0'; exitflag = trimString(line); if(!exitflag) { reverseString(line); if(trie_check(&head,line)) { fprintf(result,"yes\n"); } else { fprintf(result,"no\n"); } } } trie_destroy(&head); }
void leftRotateString(char* s,int length, int pos) { reverseString(s,0,m-1); reverseString(s,m,n-1); reverseString(s,0,n-1); }
string reverseString(string s) { int slen = s.size(); if (slen <= 1) return s; string left = s.substr(0, slen/2);//substr() func can get the sub string string right = s.substr(slen/2, slen); return reverseString(right) + reverseString(left); // use plus to insert left after right }
string Solution::addBinary(string A, string B) { string c=""; int n=A.length(); int m=B.length(); reverseString(A); reverseString(B); int i=0; int sum=0,carry=0; while(i<n && i<m){ sum=(A[i]-'0')+(B[i]-'0')+carry; carry=sum/2; sum%=2; c+=(sum+'0'); i++; } while(i<n){ sum=(A[i]-'0')+carry; carry=sum/2; sum%=2; c+=(sum+'0'); i++; } while(i<m){ sum=(B[i]-'0')+carry; carry=sum/2; sum%=2; c+=(sum+'0'); i++; } if(carry>0) c+='1'; reverseString(c); return c; }
void testreverseString() { char *a = (char *)malloc(10 * sizeof(char)); strcpy(a, "abc def"); printf("%s\n", reverseString(a)); free(a); }
int main() { char str[] = "Banana"; printf("%s\n", str); reverseString(str); printf("%s\n", str); }
/*Checks if a given string is a palindrome*/ void isPalindrome(char *input) { int i=0; char *reverse; reverse=reverseString(input); int palindrome=1; int j=0; while(input[i] !='\0') { if(input[i] != reverse[j]) { palindrome=0; break; } j++; i++; } if(palindrome==1) printf("TRUE\n"); else printf("FALSE\n"); }
void str_words_in_rev(char *input, int len){ char* begin = input; char* temp = input; while(*temp){ temp++; if(*temp == '\0') reverseString(begin, temp - 1); else if(*temp == ' '){ reverseString(begin, temp - 1); begin = temp + 1; } } reverseString(input, temp - 1); }
int main() { char s[] = "abcdefg"; char *result = reverseString(s); printf("%s",result); return 0; }
int main() { char *s = strdup("helo"); char *p = reverseString(s); printf("%s\n", p); free(s); return 0; }
void Solution::reverseWords(string &s){ uniformString(s); unsigned len = s.length(); if (len<=2) return; reverseString(s, 0, len-1); unsigned start = 0; while (start<len){ unsigned end = start; while (end<len && s[end]!=' ') end++; if (end-start>=2) reverseString(s, start, end-1); start = end+1; } }
void main() { char *str = getString(); printf("OriginalString(%s)", str ); printf("revString(%s)", reverseString(str) ); //reverseString(str); }
void Dictionary::loadDict( string dictPath, bool backwardFA) { hashMapDict = map<string, string>(); ifstream dicFile(dictPath.c_str(), ios::binary); string line; if (dicFile.is_open()) { while (getline(dicFile, line)) { istringstream iss(line); string phones, word; iss >> word; ostringstream oss; oss << iss.rdbuf(); phones = oss.str(); phones.erase(std::remove(phones.begin(), phones.end(), '\t'), phones.end()); // remove tab if any phones.erase(std::remove(phones.begin(), phones.end(), '\n'), phones.end()); // remove newline if any phones.erase(std::remove(phones.begin(), phones.end(), '\r'), phones.end()); // remove newline if any // cout << word<< " " << phones<<endl; if (backwardFA) { hashMapDict[word] = reverseString(phones); } else { hashMapDict[word] = phones; } } dicFile.close(); } else {
int main(void) { int a = 3; int b = 5; printf("%d\t%d\n", a, b); swap_int(&a, &b ); printf("%d\t%d\n", a, b); char s1[] = "a"; char s2[] = "b"; printf("%s\t%s\n", &s1[0], &s2[0]); swap_char(s1, s2 ); printf("%s\t%s\n", &s1[0], &s2[0]); char s3[] = "abcdefg"; printf("%s\n", &s3[0]); reverseString(s3); printf("%s\n", &s3[0]); return 0; }
int main() { const char* string = (char *)malloc(50); const char* sub = (char *)malloc(19); int a = 8; float b = 9.4444; int c = 2; int d; printf("Before swapping: a = %d c = %d\n", a , c); a = a + c; c = a - c; a = a - c; printf("After swapping: a = %d c = %d\n", a , c); string = "This is the company namded apple inc"; sub = "is"; printf("Substring: %d\n", isSubstring(string, sub)); printf("Position of the MSB is: %d\n", msbPos(3)); printf("Is bit set: %d\n", isBitset(8,4)); printf("%x to little endian %x\n", a, BigToLittle(a)); printf("%d is a power of 2: %d\n", a ,isPowerof2(a)); printf("%s after reversed: %s\n", string, reverse(string)); printf("%s is palindrome: %d\n", string, isPalindrome(string)); printf("%d is a prime number: %d\n",a, isPrimeNumber(a)); primeNumber(20); printf("\nFibonacci Series: "); fibonacci(15); printf("\n"); printf("Factorial of %d: %d\n", a, factorial(a)); printf("%s after reversing: %s\n", string, reverseString(string)); printf("Nearest int of %f is: %d\n", b, nearestInt(b)); printf("%d", (-1%8)); return 0; }
int main (int argc, char *argv[]) { char buffer[MAX_STR_LEN+1]; char *str; int index = 0; printf("Enter string to reverse:\n"); str = fgets(buffer, MAX_STR_LEN+1, stdin); if (str == NULL) { printf("Error reading string from stdin. Quitting...\n"); return 0; } /* strip last new line, if read in from std input */ index = strlen(buffer) - 1; if (buffer[index] == '\n') buffer[index] = '\0'; reverseString(buffer); printf("Reversed string:\n%s\n", buffer); return 0; }
void itoafixedversionminwidth(int number, char newString[], int minwidth){ int i, sign; if((sign = number) < 0) number = -number; i = 0; do{ int absValue = abs(number%10)+'0'; printf("...Processing absolute value of %d %% 10 + 0 = %c\n",number, absValue); if(minwidth) --minwidth; newString[i++] = abs(number % 10) + '0'; //fix to get correct character printf("Divide by 10...original value: %d\n", number/10); } while(number /= 10); // fixed part as we will end up with negative in two's complement if(sign < 0) newString[i++] = '-', --minwidth; if(minwidth) while(minwidth--) newString[i++] = ' '; newString[i++] = '\0'; reverseString(newString); }
void test_reverseString_murkerjee_should_return_eejrekrum(void) { char *result; result = reverseString("murkerjee"); TEST_ASSERT_EQUAL_STRING("eejrekrum", result); }
int main() { // test char str1[] = "This is a test string"; reverseString(str1); assert(strcmp(str1, "gnirts tset a si sihT") == 0); return 0; }
void trie(FILE *pool,FILE *check,FILE *result) { clock_t start,end,start1; start = clock(); TRIE *head = trie_create(); char line[BUFFERSIZE]; int count=0; int i=0; int exitflag=0; while(fgets(line,BUFFERSIZE,pool)) { /*delete the useless character '\r'*/ exitflag = trimString(line); if(!exitflag){ reverseString(line); trie_add(&head,line); // if(!(++count%100000)){ // end = clock(); // printf("%d,%f \n",count++,(double)(end -start)/CLOCKS_PER_SEC); // } }else{ /*printf("Error email %s",line);*/ continue; } } //end = clock(); //printf("Creating tree using %f\n",(double)(end -start)/CLOCKS_PER_SEC); start1 = clock(); while(fgets(line,BUFFERSIZE,check)) { i = 0; while(line[i]!='\r' && line[i]!='\n') i++; line[i] = '\0'; exitflag = trimString(line); if(!exitflag){ reverseString(line); if(trie_check(&head,line)) { fprintf(result,"yes\n"); }else { fprintf(result,"no\n"); } } } trie_destroy(&head); end = clock(); printf("%f\n",(double)(end -start)/CLOCKS_PER_SEC); }
void reverseString(char *str) { if (*str) { reverseString(str+1); printf("%c", *str); } }
int main(){ char chPtr[] = "I am a student."; int len = strlen(chPtr); reverseString(chPtr,0,len-1); printf("%s\n",chPtr); int start = 0, end = 0; while(chPtr[end] != '\0'){ if(chPtr[end] == ' '){ reverseString(chPtr,start,end-1); start = end + 1; } end++; } printf("%s\n",chPtr); return 0; }
// Testing int main() { char string[5] = "abcd"; printf("string %s ---> ", string); reverseString(string); printf("%s\n", string); return 0; }
int main(){ int i; char a[]="abcdefg"; int size=sizeof(a)/sizeof(a[0]); printf("%s\n",a); reverseString(a); printf("%s\n",a); return 0; }
int main(){ char str[] = "this is a test"; char *ptr = str; int len = strlen(str); char *str2 = (char*)malloc(len*sizeof(char)); reverseString(str, str2); printf("\n %s", str2); getchar(); }
void reverseString(char *str, char *str2) { static int i =0; if(*str=='\0'){ return; } reverseString(str+1,str2); str2[i++]=*str; // printf("%s", str2); }
int main(int argc, char *argv[]) { int val = 9; char str[]={"knuj"}; printf("Print numbers in reverse order using recursion input %d(default)",val); p_reverse(val); printf("Reverse of : %s is : ",str); reverseString(str); printf("\n"); return (0); }
void reverseWords(char *str) { int i = 0, j = 0; while (true) { if (str[i + 1] == '\0' || str[i + 1] == ' ') { reverseString(str, j, i); if (str[i + 1] == '\0') { break; } j = i + 2; } ++i; } reverseString(str, 0, i); }