reverse

Reverse a string in c [duplicate]

巧了我就是萌 提交于 2019-12-13 02:30:52
问题 This question already has answers here : In-Place String Reverse in C (3 answers) Closed 6 years ago . Im trying to reverse a string in place. void reverseStr(char *str) { int i; int length; int last_pos; length = strlen(str); last_pos = length-1; for(i = 0; i < length / 2; i++) { char tmp = str[i]; str[i] = str[last_pos - i]; str[last_pos - i] = tmp; } } Program received signal SIGSEGV, Segmentation fault. 0x0000000000400893 in reverseStr (str=0x400974 "Haus") at main.c:102 102 str[i] = str

Reversing an array using a stack

∥☆過路亽.° 提交于 2019-12-12 22:09:02
问题 I am attempting to reverse an array using a stack. However, I get an error on arr[i] = stack.top(); , and the suggestion to resolve it in Eclipse is to change it to arr[i] = stack.pop(); or to add a cast. Is there another way about this or have I made a mistake? I see tutorials and questions asking about how to reverse a string using a stack and I have tried to reverse an array using the same logic, but I'm not entirely sure where I'm going wrong. public static void reverse(String[] arr){

How to reverse a linked list - detailed explanation

旧巷老猫 提交于 2019-12-12 18:34:57
问题 Can anyone share a link to the code which explains of how to reverse a linked list? Or could somebody explain the code snippet below? I've tried to draw/write it, but still don't get how nodes get reversed. public void reverseList() { Node reversedPart = null; Node current = head; while (current != null) { Node next = current.next; current.next = reversedPart; reversedPart = current; current = next; } head = reversedPart; } 回答1: Let's take a look into a simple example: 1 > 2 > 3 > 4 > 5 >

String array reverse Java

笑着哭i 提交于 2019-12-12 18:22:41
问题 I am trying to reverse all the strings in an array in java, but seem to over write all of them with the first. private static void palindrome(String[] s) { int flag=0; String reverse; for(int i=0;i<n;i++) // n is declared globally as number of strings { reverse=""; for (int j=s[i].length()-1;i>=0;i--) reverse=reverse+s[i].charAt(j); if(s[i].equals(reverse)) { System.out.println(s[i]); flag=1; } } if(flag==0) System.out.println("There are no palindromic strings"); } 回答1: Looks like you used i

Printing a C string in reverse without using pointers?

自作多情 提交于 2019-12-12 18:19:16
问题 Is there a way to print a string of fixed size in reverse without using pointers? #include<stdio.h> main() { char buffer[10]; scanf("%s", buffer); // need to print buffer in reverse without using pointers?? } 回答1: A lovely K&R function to reverse your string in-place before printing it, perhaps? #include <stdio.h> #include <string.h> void strrev(char *s) { int tmp, i, j; for (i = 0, j = strlen(s) - 1; i < j; i++, j--) { tmp = s[i]; s[i] = s[j]; s[j] = tmp; } } int main(int argc, const char

Reversing Array in C?

耗尽温柔 提交于 2019-12-12 11:15:54
问题 Hi i trying to implement a reverse array code but it doesnt seem to work and im really not sure why. The For loop just doesnt seem to work. I dont know why because the logic seems pretty right to me. #include <stdio.h> #include <string.h> void reverse(char, int); int main() { char a[100]; gets(a); reverse(a, strlen(a)-1); printf("%s\n",a); getchar(); getchar(); getchar(); return 0; } void reverse(char ar[], int n) { char c; int i = 0; printf("n = %d" , n); for ( i = 0; i >= n ; i++){ c = ar[i

Reverse URL issue

≡放荡痞女 提交于 2019-12-12 10:57:41
问题 I have a django model defined as from utils.utils import APIModel from django.db import models from django.core.urlresolvers import reverse class DjangoJobPosting(APIModel): title = models.CharField(max_length=50) description = models.TextField() company = models.CharField(max_length=25) def get_absolute_url(self): return reverse('jobs.views.JobDetail', args=[self.pk]) with a view from restless.views import Endpoint from restless.models import serialize from .models import * from utils.utils

Reverse a string using recursion

老子叫甜甜 提交于 2019-12-12 10:18:11
问题 I got this code from the internet but I couldnt get the whole code. for example if(*str) . What does this code mean? and also can a string be returned? I thought that an array in main can be changed directly in a function but here its been returned.. #include<stdio.h> #define MAX 100 char* getReverse(char[]); int main(){ char str[MAX],*rev; printf("Enter any string: "); scanf("%s",str); rev = getReverse(str); printf("Reversed string is: %s\n\n",rev); return 0; } char* getReverse(char str[]){

Recursively reversing a string in C?

ぃ、小莉子 提交于 2019-12-12 09:24:35
问题 I have to reverse a string in a recursive function, but I cannot use loops or strlen to find where the end of the string is. Then I have to pass the reversed string back to main and copy it to a new file. Here's what I have so far: int reverse(char *str, char *strnew, int p) { char temp=str[p]; if(temp=='\0' || temp=='\n') { strnew=str; return p; } else { reverse(str++, strnew, ++p); p--; strnew[p]=str[p]; printf("strnew: %c\n", strnew[p]); return 0; } } int main(int argc, char *argv[]) {

Does Stack<> constructor reverse the stack when being initialized from other one?

試著忘記壹切 提交于 2019-12-12 08:50:18
问题 Here is the code: var s = new Stack<int>(); s.Push(1); s.Push(2); s.Push(3); s.Push(4); var ns = new Stack<int>(s); var nss = new Stack<int>(new Stack<int>(s)); and then let's see the result tbLog.Text += "s stack:"; while(s.Count > 0) { tbLog.Text += s.Pop() + ","; } tbLog.Text += Environment.NewLine; tbLog.Text += "ns stack:"; while (ns.Count > 0) { tbLog.Text += ns.Pop() + ","; } tbLog.Text += Environment.NewLine; tbLog.Text += "nss stack:"; while (nss.Count > 0) { tbLog.Text += nss.Pop()