reverse

Reverse a string using a recursive function

此生再无相见时 提交于 2019-12-02 04:28:21
问题 I am currently studying C and I can't get past this exercise. I must create a recursive function to reverse string1 into string2 . Here is my code. I would gladly appreciate your help. #include <stdio.h> #define MAX 100 void reverse(char s1[],char s2[],int n,int j); int main() { char string1[MAX]="How Are You Mate"; char string2[MAX]=""; int n=0; int i=0; int j=0; for(i=0;string1[i]!='\0';i++) n++; reverse(string1,string2,n,j); printf("String-a normal:\n%s\n",string1); printf("String-a

Reverse a string using a recursive function

浪尽此生 提交于 2019-12-02 02:21:41
I am currently studying C and I can't get past this exercise. I must create a recursive function to reverse string1 into string2 . Here is my code. I would gladly appreciate your help. #include <stdio.h> #define MAX 100 void reverse(char s1[],char s2[],int n,int j); int main() { char string1[MAX]="How Are You Mate"; char string2[MAX]=""; int n=0; int i=0; int j=0; for(i=0;string1[i]!='\0';i++) n++; reverse(string1,string2,n,j); printf("String-a normal:\n%s\n",string1); printf("String-a reverse:\n%s\n",string2); return 0; } void reverse(char s1[],char s2[],int n,int j) { if(n>0) { s2[j]=s1[n];

Reverse a string each two characters with Ruby

微笑、不失礼 提交于 2019-12-02 02:20:37
问题 I want to reverse a string each two characters with Ruby. The input: "0123456789abcdef" The output that I expect: "efcdab8967452301" 回答1: I'm not sure if this is the best way to do it: "0123456789abcdef".scan(/../).reverse.join == "efcdab8967452301" 回答2: "0123456789abcdef".gsub(/../, &:reverse).reverse # => "efcdab8967452301" 回答3: You can try: "0123456789abcdef".chars.each_slice(2).to_a.reverse.join 回答4: I like all the solutions, especially @sawa's, but now, six hours later, its a challenge

How can I reverse a section of a list using a loop in Python?

只愿长相守 提交于 2019-12-02 01:38:55
I'm in need of a little help reversing a section of a list in Python using a loop. I have a list: mylist = ['a', 'b', 'c', 'd', 'e', 'f'] Also have an index number, this number will tell where to start the reversing. For example, if the reverse-index number is 3, it needs to be something like this: ['d', 'c', 'b', 'a', 'e', 'f'] What I currently have: def list_section_reverse(list1, reverse_index): print("In function reverse()") n_list = [] for item in range( len(list1) ): n_list.append( (list1[(reverse_index - 1) - item]) ) item += 1 return n_list mylist = ['a', 'b', 'c', 'd', 'e', 'f'] print

reverse second sort characteristic in python sorted()

こ雲淡風輕ζ 提交于 2019-12-02 01:21:50
问题 I'm trying to sort a list and I can't figure out to reverse the order of a second sorting characteristic. import math ps = {1:(1,1),2:(3,2),3:(3,-3),4:(-3,4),5:(-2,-2),6:(3,3),7:(1,-1)} l = [] for x in range(1,8): l.append((math.atan2(ps[x][1],ps[x][0]),ps[x])) for c in sorted(l, key = lambda t:(t[0], math.sqrt((0-t[1][0])**2 + (0-t[1][1])**2)),reverse = True): print(c) The first sort characteristic is sorting by angle, the second sorts by distance from the origin if the angle is equal. Does

Reverse a number without making it a string in Javascript [duplicate]

会有一股神秘感。 提交于 2019-12-02 00:20:40
This question already has an answer here: JavaScript: How to reverse a number? 8 answers Can anyone show me where I'm going wrong in my code please? I'm trying to reverse a number without changing it to a string. I've been searching google and looked through the previous questions asked about this topic and from what I can see my code mirrors the other answers. I've only been able to find code in Java, C, or C++ that do not use the to string method. In my attempts, when I run the code in the browser console it either gives me an answer of "Infinity" or crashes my browser. Here's my code...

Reverse a string each two characters with Ruby

吃可爱长大的小学妹 提交于 2019-12-01 23:34:21
I want to reverse a string each two characters with Ruby. The input: "0123456789abcdef" The output that I expect: "efcdab8967452301" I'm not sure if this is the best way to do it: "0123456789abcdef".scan(/../).reverse.join == "efcdab8967452301" "0123456789abcdef".gsub(/../, &:reverse).reverse # => "efcdab8967452301" You can try: "0123456789abcdef".chars.each_slice(2).to_a.reverse.join I like all the solutions, especially @sawa's, but now, six hours later, its a challenge to come up with something different. In the interest of diversity, I offer a way that uses pairwise substitution. It's

Use SUBSTRING AND CHARINDEX to get last complete word in field

て烟熏妆下的殇ゞ 提交于 2019-12-01 22:11:37
问题 I have a field in my database that I plan to truncate, for reporting purposes, to 50 characters with the statement below. SELECT (CASE WHEN (LEN(Notes) > 50) THEN SUBSTRING(Notes, 0, 50) + '...' WHEN (LEN(Notes) < 50) THEN SUBSTRING(Notes, 0, LEN(Notes)) + '...' ELSE 'NO NOTES WERE ENTERED' END) AS Notes FROM MyTable This works great, however, I would like to complete the last word in the notes field so that a word isn't cut off so I would like to use CHARINDEX, SUBSTRING, REVERSE and

reverse second sort characteristic in python sorted()

好久不见. 提交于 2019-12-01 22:09:16
I'm trying to sort a list and I can't figure out to reverse the order of a second sorting characteristic. import math ps = {1:(1,1),2:(3,2),3:(3,-3),4:(-3,4),5:(-2,-2),6:(3,3),7:(1,-1)} l = [] for x in range(1,8): l.append((math.atan2(ps[x][1],ps[x][0]),ps[x])) for c in sorted(l, key = lambda t:(t[0], math.sqrt((0-t[1][0])**2 + (0-t[1][1])**2)),reverse = True): print(c) The first sort characteristic is sorting by angle, the second sorts by distance from the origin if the angle is equal. Does anyone know how to do this. Thanks in advance for the help. Put a minus sign in front of the second

Reverse string method

£可爱£侵袭症+ 提交于 2019-12-01 21:15:21
问题 I am trying to solve the following problem but how do write the method that accepts String as an argument? Write a method named printReverse that accepts a String as an argument and prints the characters in the opposite order. If the empty string is passed as an argument, the method should produce no output. Be sure to write a main method that convincingly demonstrates your program in action. Do not use the reverse method of the StringBuilder or StringBuffer class! So far I have solved it in