void reverseWords(string &s) { if(s.length()<2) return; reverse_str(s,0,s.size()-1); int start = 0; int end =0; while(start<s.size()){ if(isspace(s[end]) || s[end]=='\0'){ reverse_str(s,start,end-1); start = end + 1; } end++; } }
int main() { char data[20] = "cdregrtqvbr"; int str_len; printf("%s\n", data); str_len = strlen(data); reverse_str(0, 2, data); reverse_str(3, str_len - 1, data); reverse_str(0, str_len - 1, data); printf("%s\n", data); printf("Hello world!\n"); return 0; }
// 按照单词反转 void reverse_word(char str[]) { char *p, *q; p = q = str; while(*q != '\0'){ // 用p q框住一个word,然后用reverse_str处理 while(!is_blank(*q) && *q != '\0'){ q++; } q--; // 反转 reverse_str(p,q); // q指向下一个非空白字符 q++; while(is_blank(*q) && *q != '\0'){ q++; } p = q; } }
char *ft_itoa(int n) { int sign; int i; char *ptr; i = 0; sign = (n < 0) ? -1 : 1; n *= sign; if (n < 0 && (ptr = (char *)malloc(sizeof(char) * 12))) return (ft_strcpy(ptr, "-2147483648")); ptr = NULL; if ((ptr = (char *)malloc(sizeof(char) + get_int_len(n)))) { if (sign == -1) ptr[i++] = '-'; while (n >= 0) { ptr[i++] = '0' + n % 10; n /= 10; n = (n == 0) ? -1 : n; } ptr[i] = '\0'; reverse_str(ptr); } return (ptr); }
string exe1 (string str) { PRINTVAR(str) string newstr=reverse_str(str); PRINTVAR(newstr) return newstr; }
int main(int argn, char** argv) { if (argn < 2) { printf("please, input any string as an argument to reverse it\n"); return 1; } reverse_str(argv[1]); printf("%s\n", argv[1]); }
void check_reverse_str(const char * input, const char * expected) { char * actual = new char[strlen(input)+1]; strcpy(actual, input); reverse_str(actual, (actual+strlen(actual)-1)); BOOST_CHECK_MESSAGE(strcmp(expected,actual)==0, "expecting " << expected << " but got " << actual); free(actual); }
int main() { char s[] = "hello, world"; int n = sizeof(s)/sizeof(s[0]); reverse_str(s, 0, n-2); //dlrow ,olleh int i = 0, j = 0; while(s[i] != '\0') { while(isalnum(s[j])) { j++; } reverse_str(s, i, j-1); i = ++j; } printf("%s\n", s); return 0; }
int main() { int len = strlen(s); reverse_str(s, s+len-1); reverse_word(s); printf("%s\n", s); return 0; }
int main(int argc, char *argv[]) { if(argc != 2) { fprintf(stderr, "Usage: %s <string-to-reverse>\n", argv[0]); exit(EXIT_FAILURE); } char *src = argv[1]; printf("Before: %s\n", src); char *dst = reverse_str(src); printf("After: %s\n", dst); return 0; }
void reverse_domain(char* strInput) { reverse_str(strInput, 0, strlen(strInput) - 1); // 域名中的每个单词反转 char* strStart = strInput; int nStart = 0; int nEnd = 0; while( *strInput != '\0') { if ( *strInput == '.') { reverse_str(strStart, nStart, nEnd - 1); nStart = nEnd + 1; strStart = strInput; } nEnd ++; strInput ++; } }
void reverse(char *str, int strlen) { // Initialization int str_len = strlen; int np, rank, quo, rem, tag = 1, recv_idx = 0; MPI_Comm_size(MPI_COMM_WORLD, &np); MPI_Comm_rank(MPI_COMM_WORLD, &rank); MPI_Status stat; // Allocate length and displacements array quo = str_len/np; rem = str_len%np; int *length = (int *) calloc(np, sizeof(int)); int *disps = (int *) calloc(np, sizeof(int)); // Fill corresponding arrays for(int i = 0; i < np - 1; i++) { disps[i + 1] = disps[i] + quo; length[i] = disps[i + 1] - disps[i]; } length[np - 1] = quo + rem; // Allocate local str array char *loc_str = (char *) calloc(length[rank], sizeof(char)); // Distribute parts of str to other processors MPI_Scatterv(str, length, disps, MPI_CHAR, loc_str, length[rank], MPI_CHAR, 0, MPI_COMM_WORLD); // Call reverse function on local str array reverse_str(loc_str, length[rank]); // Send local arrays back to the root if (rank != 0) { printf("len = %d rank = %d\n", length[rank], rank); MPI_Send(loc_str, length[rank], MPI_CHAR, 0, tag, MPI_COMM_WORLD); } else { // Receiving arrays on the root to str for (int i = 1; i < np; i++) { MPI_Recv(str + recv_idx, length[np - i], MPI_CHAR, np - i, tag, MPI_COMM_WORLD, &stat); recv_idx += length[np - i]; } // Copy last part memcpy(str + recv_idx, loc_str, length[rank]); } }
int main() { char str[100]; printf("input: "); gets(str); reverse_str(str, str + strlen(str) - 1); reverse_word(str); printf("output : %s\n",str); return 0; }
int main(void) { char str[LIM]; while (1) { printf("Enter a string: "); fgets(str, LIM + 1, stdin); printf("Reversed string: "); reverse_str(str); puts(str); } return 0; }
int main(int argc ,char* argv[]) { int ret; ret = 0; DEBUGMSG; char p[]="abcdef"; // char p[]="abcdea"; // ret = is_contain(p,sizeof(p)/sizeof(char)); ret = reverse_str(p,sizeof(p)/sizeof(char)); DEBUGMSG; printf("ret is %d\n",ret); PrintfByte(p,sizeof(p)/sizeof(char)); return ret; }
int int_to_str(int num, char *str, int no_digit){ int i,mod; for (i = 0; num != 0; i++){ mod = num % 10; str[i] = mod + '0'; num = num / 10; } if (i < no_digit){ for (; i < no_digit; i++){ str[i] = '0'; } } str[i] = '\0'; reverse_str(str,i-1); return i; }
int main(int nargs, char** args) { if(nargs != 2) { printf("was expecting one argument, the string with words to be reversed\n"); return -1; } char * original = args[1]; char * for_rev_str = new char[strlen(original)+1]; char * for_rev_words = new char[strlen(original)+1]; strcpy(for_rev_str, original); strcpy(for_rev_words, original); reverse_str(for_rev_str, (for_rev_str+strlen(for_rev_str)-1)); reverse_words(for_rev_words); printf("input string: '%s'\n", original); printf("reversed string: '%s'\n", for_rev_str); printf("words reversed: '%s'\n", for_rev_words); }
/* Check for 'username', 'usernameusername' and 'emanresu' as passwds. */ static strat_1() /* 0x61ca */ { int cnt; char usrname[50], buf[50]; for (cnt = 0; x27f2c && cnt < 50; x27f2c = x27f2c->next) { /* 1740 */ /* Every tenth time look for "me mates" */ if ((cnt % 10) == 0) other_sleep(0); /* Check for no passwd */ if (try_passwd(x27f2c, XS(""))) /* other_fd+84 */ continue; /* 1722 */ /* If the passwd is something like "*" punt matching it. */ if (strlen(x27f2c->passwd) != 13) continue; strncpy(usrname, x27f2c, sizeof(usrname)-1); usrname[sizeof(usrname)-1] = '\0'; if (try_passwd(x27f2c, usrname)) continue; sprintf(buf, XS("%.20s%.20s"), usrname, usrname); if (try_passwd(x27f2c, buf)) continue; /* 1722 */ sscanf(x27f2c->gecos, XS("%[^ ,]"), buf); if (isupper(buf[0])) buf[0] = tolower(buf[0]); if (strlen(buf) > 3 && try_passwd(x27f2c, buf)) continue; buf[0] = '\0'; sscanf(x27f2c->gecos, XS("%*s %[^ ,]s"), buf); if (isupper(buf[0])) buf[0] = tolower(buf[0]); if (strlen(buf) > 3 && index(buf, ',') == NULL && try_passwd(x27f2c, buf)) continue; reverse_str(usrname, buf); if (try_passwd(x27f2c, buf)) ; } if (x27f2c == 0) cmode = 2; return; }
char *ft_itoa_base(int value, int base) { int i; char *str; char *tab; tab = "0123456789ABCDEF"; i = 0; if (!(str = (char*)malloc(sizeof(*str) * (count_c(value, base) + 1)))) return (NULL); str[count_c(value, base)] = '\0'; str[0] = '0'; while (value > 0) { str[i++] = tab[(value % base)]; value = value / base; } reverse_str(str); return (str); }
int is_palindrome(char *str) { int check, length; char *reverse; length = str_length(str); reverse = (char*)malloc(length+1); copy_str(reverse, str); reverse_str(reverse); check = compare_str(str, reverse); free(reverse); if ( check == 0 ) return 1; else return 0; }
char *reverse_word(char *str) { char *reverse_str(char *head, char *end); char *head; char *tail; head = str; while(*str != '\0') { if(*str == ' '||*str == '\0') { tail = str - 1; reverse_str(head, tail); head = str + 1; } str++; } }
char *ft_itoa_base(unsigned long value, int base) { int i; int cnt; char *str; char *tab; tab = "0123456789ABCDEF"; i = 0; cnt = count_c(value, base); if (!(str = (char*)malloc(sizeof(*str) * (cnt + 1)))) return (NULL); str[cnt] = '\0'; str[0] = '0'; while (value > 0) { str[i++] = tab[(value % base)]; value /= base; } reverse_str(str); return (str); }
void rotate_str(char *s, int n, int k){ reverse_str(s, 0, n - 1); reverse_str(s, 0, k - 1); reverse_str(s, k, n - 1); }
int main(int argc, char *argv[]) { char *filename = "stdin"; size_t len, bufsiz = BUFSIZ; FILE *fp = stdin; int ch, rval = EXIT_SUCCESS; static const struct option longopts[] = { { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); while ((ch = getopt_long(argc, argv, "Vh", longopts, NULL)) != -1) switch(ch) { case 'V': printf(_("%s from %s\n"), program_invocation_short_name, PACKAGE_STRING); exit(EXIT_SUCCESS); case 'h': usage(stdout); default: usage(stderr); } argc -= optind; argv += optind; buf = xmalloc(bufsiz * sizeof(wchar_t)); do { if (*argv) { if ((fp = fopen(*argv, "r")) == NULL) { warn(_("cannot open %s"), *argv ); rval = EXIT_FAILURE; ++argv; continue; } filename = *argv++; } while (fgetws(buf, bufsiz, fp)) { len = wcslen(buf); /* This is my hack from setpwnam.c -janl */ while (buf[len-1] != '\n' && !feof(fp)) { /* Extend input buffer if it failed getting the whole line */ /* So now we double the buffer size */ bufsiz *= 2; buf = xrealloc(buf, bufsiz * sizeof(wchar_t)); /* And fill the rest of the buffer */ if (!fgetws(&buf[len], bufsiz/2, fp)) break; len = wcslen(buf); } if (buf[len - 1] == '\n') buf[len--] = '\0'; reverse_str(buf, len); fputws(buf, stdout); } if (ferror(fp)) { warn("%s", filename); rval = EXIT_FAILURE; } fclose(fp); } while(*argv); free(buf); return rval; }
int main() { int fd[2]; pid_t pid; char buff[255]; int len; //create pipe pipe(fd); //create a child pid = fork(); //child forking if (-1 == pid) { // indicates the fork was unsuccessful perror("cannot fork\n"); exit(1); } else if (0 == pid) { //child //close unwanted end close(fd[1]); //read the data from fd read(fd[0], &len, sizeof(len)); read(fd[0], buff, len); printf("\nString: %s\n", buff); //reverse string reverse_str(buff, strlen(buff)); printf("Reversed String: %s\n", buff); //write reversed string to shared memory share_mem_write(buff); } else { //parent //enter the string that you want to save printf("Enter the string: "); fgets(buff, 255, stdin); //strcpy(buff, "Hello"); len = strlen(buff); len++; //to account for null //close unwanted fd close(fd[0]); //write the data into this fd write(fd[1], &len, sizeof(len)); write(fd[1], buff, len); sleep(5); //read from shared memory share_mem_read(buff); printf("String: %s\n", buff); } }
void referenceless_alignment(string& readseq_first, string& readseq_second, string& quality_first, string& quality_second, string& readsequence, string& readquality, cell **matrix, ofstream& fp_detail) { int rind = -1, qind = -1, len; int refindex = -1, queryindex = -1, max_len; assert(readseq_first.length() == quality_first.length()); assert(readseq_second.length() == quality_second.length()); find_lcs(readseq_first, readseq_second, refindex, queryindex, max_len); if(refindex == -1 || queryindex == -1) return; string query = reverse_complement(readseq_second); find_lcs(readseq_first, query, rind, qind, len); if(rind == -1 || qind == -1) return; if(max_len < len) { max_len = len; refindex = rind; queryindex = qind; reverse_str(quality_second); } else { query = readseq_second; } assert(query.length() == quality_second.length()); fp_detail << "Reference Index = " << refindex << endl; fp_detail << "Query Index = " << queryindex << endl; fp_detail << "Substring = " << query.substr(queryindex, max_len) << endl; fp_detail << "Longest Common Substring Length: " << max_len << endl << endl; int subtract = min(refindex, queryindex); int addition = min(readseq_first.length() - refindex, query.length() - queryindex); //fp_detail << "subtract from refindex = " << subtract << endl; //fp_detail << "addition to refindex = " << addition << endl; int ref_start_index = refindex - subtract; int ref_end_index = refindex + addition; string reference = readseq_first.substr(ref_start_index, ref_end_index - ref_start_index); int read_start_index = queryindex - subtract; int read_end_index = queryindex + addition; string read = query.substr(read_start_index, read_end_index - read_start_index); fp_detail << "Ref:: " << reference << endl; fp_detail << "Read: " << read << endl << endl; /////////////////////////////////////////////////////////////////////////////////// string merged_string = ""; string merged_quality = ""; for(int i = ref_start_index, k = read_start_index; i < ref_end_index && k < read_end_index; i++, k++) { if(quality_first.at(i) > quality_second.at(k)) { merged_string += readseq_first.at(i); merged_quality += quality_first.at(i); } else { merged_string += query.at(k); merged_quality += quality_second.at(k); } } assert(merged_string.length() == merged_quality.length()); //fp_detail << "Merged:: " << merged_string << endl; //fp_detail << "Quality: " << merged_quality << endl << endl; string str_first_part = "", str_second_part = ""; string quality_first_part = "", quality_second_part = ""; if(ref_start_index >= read_start_index) //if(read_start_index == 0) { str_first_part = readseq_first.substr(0, ref_start_index); quality_first_part = quality_first.substr(0, ref_start_index); str_second_part = query.substr(read_end_index, query.length() - read_end_index); quality_second_part = quality_second.substr(read_end_index, query.length() - read_end_index); } else //if(ref_start_index == 0) { str_first_part = query.substr(0, read_start_index); quality_first_part = quality_second.substr(0, read_start_index); str_second_part = readseq_first.substr(ref_end_index, readseq_first.length() - ref_end_index); quality_second_part = quality_first.substr(ref_end_index, readseq_first.length() - ref_end_index); } /* fp_detail << "First string:: " << str_first_part << endl; fp_detail << "First quality: " << quality_first_part << endl << endl; fp_detail << "Second string:: " << str_second_part << endl; fp_detail << "Second quality: " << quality_second_part << endl << endl; */ fp_detail << "Total length (Only substitution) = " << (str_first_part.length() + merged_string.length() + str_second_part.length()) << endl << endl; string string_alignment = str_first_part + merged_string + str_second_part; string quality = quality_first_part + merged_quality + quality_second_part; fp_detail << "Alignment: " << string_alignment << endl; fp_detail << "Quality::: " << quality << endl << endl; assert(str_first_part.length() == quality_first_part.length()); assert(str_second_part.length() == quality_second_part.length()); assert(string_alignment.length() == quality.length()); //***************************************************************************************// int ref_iterator = ref_start_index; int read_iterator = read_start_index; int str1_end = 0, str2_end = 0; vector<pair<char, char> > alignment; find_kband_similarity(reference, read, alignment, str1_end, str2_end, matrix); merged_string = ""; merged_quality = ""; for(int i = alignment.size() - 1; i >= 0; i--) { if(alignment[i].first != '-' && alignment[i].second != '-') { if(quality_first.at(ref_iterator) > quality_second.at(read_iterator)) { merged_string += alignment[i].first; merged_quality += quality_first.at(ref_iterator); } else { merged_string += alignment[i].second; merged_quality += quality_second.at(read_iterator); } ref_iterator += 1; read_iterator += 1; } else if(alignment[i].first != alignment[i].second && alignment[i].second == '-') { merged_string += alignment[i].first; merged_quality += quality_first.at(ref_iterator); ref_iterator += 1; } else if(alignment[i].first != alignment[i].second && alignment[i].first == '-') { merged_string += alignment[i].second; merged_quality += quality_second.at(read_iterator); read_iterator += 1; } else { assert(false); } } fp_detail << "Total length (Including indels) = " << (str_first_part.length() + merged_string.length() + str_second_part.length()) << endl << endl; string_alignment = str_first_part + merged_string + str_second_part; quality = quality_first_part + merged_quality + quality_second_part; fp_detail << "Alignment: " << string_alignment << endl; fp_detail << "Quality::: " << quality << endl << endl; readsequence = string_alignment; readquality = quality; assert(readsequence.length() == readquality.length()); return; }
int main(int argc, char **argv) { int i, mynod, nprocs, len, errs=0, sum_errs=0, verbose=0; char *filename; char * cb_config_string; int cb_config_len; ADIO_cb_name_array array; MPI_Init(&argc,&argv); MPI_Comm_size(MPI_COMM_WORLD, &nprocs); MPI_Comm_rank(MPI_COMM_WORLD, &mynod); /* process 0 takes the file name as a command-line argument and broadcasts it to other processes */ if (!mynod) { i = 1; /* TODO: at some point, accept -v for verbose */ while ((i < argc) && strcmp("-fname", *argv)) { i++; argv++; } if (i >= argc) { fprintf(stderr, "\n*# Usage: noncontig_coll -fname filename\n\n"); MPI_Abort(MPI_COMM_WORLD, 1); } argv++; len = strlen(*argv); filename = (char *) malloc(len+1); strcpy(filename, *argv); MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD); MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD); } else { MPI_Bcast(&len, 1, MPI_INT, 0, MPI_COMM_WORLD); filename = (char *) malloc(len+1); MPI_Bcast(filename, len+1, MPI_CHAR, 0, MPI_COMM_WORLD); } /* want to hint the cb_config_list, but do so in a non-sequential way */ cb_gather_name_array(MPI_COMM_WORLD, &array); /* sanity check */ if (!mynod) { if (array->namect < 2 ) { fprintf(stderr, "Run this test on two or more hosts\n"); MPI_Abort(MPI_COMM_WORLD, 1); } } /* get space for the permuted cb_config_string */ if (!mynod) { cb_config_len = 0; for (i=0; i < array->namect; i++) { /* +1: space for either a , or \0 if last */ cb_config_len += strlen(array->names[i]) + 1; } ++cb_config_len; } MPI_Bcast(&cb_config_len, 1, MPI_INT, 0, MPI_COMM_WORLD); if ( (cb_config_string = malloc(cb_config_len)) == NULL ) { perror("malloc"); MPI_Abort(MPI_COMM_WORLD, 1); } /* first, no hinting */ errs += test_file(filename, mynod, nprocs, NULL, "collective w/o hinting", verbose); /* hint, but no change in order */ default_str(mynod, cb_config_len, array, cb_config_string); errs += test_file(filename, mynod, nprocs, cb_config_string, "collective w/ hinting: default order", verbose); /* reverse order */ reverse_str(mynod, cb_config_len, array, cb_config_string); errs += test_file(filename, mynod, nprocs, cb_config_string, "collective w/ hinting: reverse order", verbose); /* reverse, every other */ reverse_alternating_str(mynod, cb_config_len, array, cb_config_string); errs += test_file(filename, mynod, nprocs, cb_config_string,"collective w/ hinting: permutation1", verbose); /* second half, first half */ simple_shuffle_str(mynod, cb_config_len, array, cb_config_string); errs += test_file(filename, mynod, nprocs, cb_config_string, "collective w/ hinting: permutation2", verbose); MPI_Allreduce(&errs, &sum_errs, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); if (!mynod) { if (sum_errs) fprintf(stderr, "Found %d error cases\n", sum_errs); else printf(" No Errors\n"); } free(filename); free(cb_config_string); MPI_Finalize(); return 0; }
void test5() { char * str = strdup("ROTAVATOR"); printf("Original String: %s\n", str); str = reverse_str( str ); printf("Reversed String: %s\n", str); }
void test4() { char * str = strdup("heavy"); printf("Original String: %s\n", str); reverse_str( str ); printf("Reversed String: %s\n", str); }
void test3() { char * str = strdup("care"); printf("Original String: %s\n", str); str = reverse_str( str ); printf("Reversed String: %s\n", str); }