使用指针(必须使用unsigned *char)在C中反转一个字符串。

huangapple go评论54阅读模式
英文:

reverse a string using pointers (have to use unsigned *char) in C

问题

以下是您提供的代码的翻译部分:

我目前正在为大学的作业而工作。

任务是在一个单独的函数中反转一个字符串。函数名如下所示:

> `void string_rev(unsigned char *str);`

我的解决方案如下:

```c
void string_rev(unsigned char *str){

	int length = 0;
	int counter = 0;
	unsigned char *endptr = str;   

	for (int i = 0; str[i] != '
我目前正在为大学的作业而工作。

任务是在一个单独的函数中反转一个字符串。函数名如下所示:

> `void string_rev(unsigned char *str);`

我的解决方案如下:

```c
void string_rev(unsigned char *str){

	int length = 0;
	int counter = 0;
	unsigned char *endptr = str;   

	for (int i = 0; str[i] != '\0'; i++)  //获取字符串的长度
		length++;

	for (int i = 0; i < length; i++)     //将endptr放到字符串的末尾
		endptr++;

	while (counter < length/2){      //交换从开头到末尾的值,直到字符串的一半
		char temp = *str;
		*str = *endptr;
		*endptr = temp;
		str++;
		endptr--;
		counter++;
	}

	for (int i = 0; i<length; i++){
		printf("%c", *str);
		str++;
	}

}

int main (void){

	char *array = "Hello";
	unsigned char *ptr = (unsigned char*)array;

	string_rev(ptr);

	return 0;
}
'
; i++) //获取字符串的长度
length++; for (int i = 0; i < length; i++) //将endptr放到字符串的末尾 endptr++; while (counter < length/2){ //交换从开头到末尾的值,直到字符串的一半 char temp = *str; *str = *endptr; *endptr = temp; str++; endptr--; counter++; } for (int i = 0; i<length; i++){ printf("%c", *str); str++; } } int main (void){ char *array = "Hello"; unsigned char *ptr = (unsigned char*)array; string_rev(ptr); return 0; }

我收到的错误是“总线错误10!但我找不到错误。

这可能与unsigned char*有关,但我无法让它工作。有人可以帮忙吗?

FYI -> 我们必须使用unsigned char*!当然要使用指针!

谢谢 使用指针(必须使用unsigned *char)在C中反转一个字符串。


<details>
<summary>英文:</summary>

I am currently working on an assignment for Uni.

The task is to reverse a string in a separate function. The function-name is given like that:

&gt; `void string_rev(unsigned char *str);`

My Solution looks like this:


void string_rev(unsigned char *str){

int length = 0;
int counter = 0;
unsigned char *endptr = str;   

for (int i = 0; str[i] != &#39;
int length = 0;
int counter = 0;
unsigned char *endptr = str;   
for (int i = 0; str[i] != &#39;\0&#39;; i++)  //getting length of string
length++;
for (int i = 0; i &lt; length; i++)     //putting endptr to end of string
endptr++;
while (counter &lt; length/2){      //switch values from start to end until half of string
char temp = *str;
*str = *endptr;
*endptr = temp;
str++;
endptr--;
counter++;
}
for (int i = 0; i&lt;length; i++){
printf(&quot;%c&quot;, *str);
str++;
}
&#39;; i++) //getting length of string length++; for (int i = 0; i &lt; length; i++) //putting endptr to end of string endptr++; while (counter &lt; length/2){ //switch values from start to end until half of string char temp = *str; *str = *endptr; *endptr = temp; str++; endptr--; counter++; } for (int i = 0; i&lt;length; i++){ printf(&quot;%c&quot;, *str); str++; }

}

int main (void){

char *array = &quot;Hello&quot;;
unsigned char *ptr = (unsigned char*)array;


string_rev(ptr);


return 0;

}


The Error I get is Bus Error 10! But I can&#39;t find the mistake.

It may has to do with the unsigned char* but I don&#39;t get it to work. Can someone please help?

FYI -&gt; We have to use unsigned char*! And of course pointers!

Thank you :)




</details>


# 答案1
**得分**: 5

请用以下方式替换:

```cpp
char array[] = "Hello";
英文:

You are trying to modify a string constant. Replace

char *array = &quot;Hello&quot;;   // Not an array.

with

char array[] = &quot;Hello&quot;;

huangapple
  • 本文由 发表于 2020年1月4日 00:50:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59582311.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定