C 在自己的位置反转字符数组
我在一次采访中被问到这个问题 我应该在自己的位置反转角色数组,而不是反转整个角色数组 如果 然后我应该以这样的方式反转,输出应该是C 在自己的位置反转字符数组,c,string,pointers,C,String,Pointers,我在一次采访中被问到这个问题 我应该在自己的位置反转角色数组,而不是反转整个角色数组 如果 然后我应该以这样的方式反转,输出应该是 anhsirk si eht tseb 我无法在面试中编写代码。有人能建议我如何编写吗 它能在指针的帮助下完成吗 如果面试官没有告诉我将其反转到自己的位置,那么使用另一个数组中的字符将很容易处理,在反转后将有新的字符串?面试官都不能为此编写代码。 char *ch="krishna is the best"; 您不能更改内存的只读部分中的数据,并且ch指
anhsirk si eht tseb
我无法在面试中编写代码。有人能建议我如何编写吗
它能在指针的帮助下完成吗
如果面试官没有告诉我将其反转到自己的位置,那么使用另一个数组中的字符将很容易处理,在反转后将有新的字符串?面试官都不能为此编写代码。
char *ch="krishna is the best";
您不能更改内存的只读部分中的数据,并且ch
指向只读内存
更新:-
N1548的摘录(§6.7.9)
例8声明
chars[]=“abc”,t[3]=“abc”代码>
定义“普通”字符数组对象s和t,其元素用字符串文字初始化。
此声明与
相同
chars[]={'a','b','c','\0'},
t[]={'a','b','c'}代码>
数组的内容是可修改的。
另一方面,声明
char*p=“abc”代码>
使用类型“指向字符的指针”
定义p,并将其初始化为指向类型为“字符数组”
长度为4,其元素用字符串文字初始化。如果试图使用p
来
修改数组的内容,行为未定义
您可以看到在这种数据类型上应用交换是危险的
建议将代码编写为:-
charch[]=“奎师那是最好的”代码>
然后在每次遇到空格字符时应用。现在不确定具体的代码,但下面是我将使用的方法(假设您能够重写原始变量)
1) 使用空格作为分隔符将字符串拆分为单词数组
2) 循环遍历数组并按相反顺序对单词排序
3) 重新生成字符串并重新分配给变量。如果我理解正确,这听起来并不难。伪代码:
let p = ch
while *p != '\0'
while *p is whitespace
++p
let q = last word character starting from p
reverse the bytes between p and q
let p = q + 1
一旦有了指向开始和结束的指针,字节范围的反转就很简单了。只需循环一半的距离,交换字节
当然,正如其他地方指出的,我假设ch
中的缓冲区实际上是可修改的,这需要更改您显示的代码。您可以逐字反转它
只需读取字符串,直到''(空格)
即。您将获得krishna
并反转此字符串,然后继续读取原始字符串,直到另一个''(空格)
并继续反转该字符串。以下是使用XOR的就地交换:
void reverseStr(char *string)
{
char *start = string;
char *end = string + strlen(string) - 1;
while (end > start) {
if (*start != *end)
{
*start = *start ^ *end;
*end = *start ^ *end;
*start = *start ^ *end;
}
start++;
end--;
}
}
当然,这是假设string
在可写内存中,所以不要抱怨它不是
如果你需要先把单词分开,给我几分钟时间,我会写一些东西
编辑:
对于用空格分隔的单词(0x20
),以下代码应适用:
void reverseStr(char *string)
{
// pointer to start of word
char *wordStart = string;
// pointer to end of word
char *wordEnd = NULL;
// whether we should stop or not
char stop = 0;
while (!stop)
{
// find the end of the first word
wordEnd = strchr(wordStart, ' ');
if (wordEnd == NULL)
{
// if we didn't a word, then search for the end of the string, then stop after this iteration
wordEnd = strchr(wordStart, '\0');
stop = 1; // last word in string
}
// in place XOR swap
char *start = wordStart;
char *end = wordEnd - 1; // -1 for the space
while (end > start) {
if (*start != *end)
{
*start = *start ^ *end;
*end = *start ^ *end;
*start = *start ^ *end;
}
start++;
end--;
}
wordStart = wordEnd + 1; // +1 for the space
}
}
不行,这是指向只读字符串文字的指针。让我们假设你的面试官知道C,并写了以下内容:
char str[]="krishna is the best";
然后你可以这样做:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char* str_reverse_word (char* str)
{
char* begin;
char* end;
char* the_end;
char tmp;
while(isspace(*str)) /* remove leading spaces from the string*/
{
str++;
}
begin = str;
end = str;
while(!isspace(*end) && *end != '\0') /* find the end of the sub string */
{
end++;
}
the_end = end; /* save this location and return it later */
end--; /* move back 1 step to point at the last valid character */
while(begin < end)
{
tmp = *begin;
*begin = *end;
*end = tmp;
begin++;
end--;
}
return the_end;
}
void str_reverse_sentence (char* str)
{
do
{
str = str_reverse_word(str);
} while (*str != '\0');
}
int main (void)
{
char str[]="krishna is the best";
str_reverse_sentence (str);
puts(str);
}
#包括
#包括
#包括
char*str\u反向词(char*str)
{
字符*开始;
字符*结束;
char*_端;
char-tmp;
while(isspace(*str))/*从字符串中删除前导空格*/
{
str++;
}
begin=str;
end=str;
而(!isspace(*end)&&&*end!='\0')/*查找子字符串的结尾*/
{
end++;
}
_end=end;/*保存此位置并稍后返回*/
结束--;/*向后移动一步,指向最后一个有效字符*/
while(开始<结束)
{
tmp=*开始;
*开始=*结束;
*end=tmp;
begin++;
结束--;
}
返回_端;
}
void str_倒装句(char*str)
{
做
{
str=str\u反义词(str);
}而(*str!='\0');
}
内部主(空)
{
char str[]=“克里希纳是最好的”;
str_倒转句(str);
put(str);
}
字符串是否真的必须在适当的位置反转,还是必须反转的只是输出
如果是前者,那么你就有问题了。如果声明真的是
char *ch = "krishna is the best";
然后您试图修改一个字符串文字,而试图修改一个字符串文字时的行为是未定义的。如果您在一个将字符串文本存储在只读内存中的平台上工作,您将得到一个运行时错误。您可能需要将声明更改为
char ch[] = "krishna is the best";
或者分配一个动态缓冲区并将字符串的内容复制到其中
char *ch = "krishna is the best";
char *buf = malloc(strlen(ch) + 1);
if (buf)
{
strcpy(buf, ch);
// reverse the contents of buf
}
为了在适当的位置完成反转
如果只是输出需要反转,那么存储实际上并不重要,您只需要几个指针来跟踪每个子字符串的开始和结束。例如:
#include <stdio.h>
#include <string.h>
int main(void)
{
char *ch = "krishna is the best";
char *start, *end;
// point to the beginning of the string
start = ch;
// find the next space in the string
end = strchr(start, ' ');
// while there are more spaces in the string
while (end != NULL)
{
// set up a temporary pointer, starting at the space following the
// current word
char *p = end;
// while aren't at the beginning of the current word, decrement the
// pointer and print the character it points to
while (p-- != start)
putchar(*p);
putchar(' ');
// find the next space character, starting at the character
// following the previous space character.
start = end + 1;
end = strchr(start, ' ');
}
// We didn't find another space character, meaning we're at the start of
// the last word in the string. We find the end by adding the length of the
// last word to the start pointer.
end = start + strlen(start);
// Work our way back to the start of the word, printing
// each character.
while (end-- != start)
putchar(*end);
putchar('\n');
fflush(stdout);
return 0;
}
#包括
#包括
内部主(空)
{
char*ch=“克里希纳是最好的”;
字符*开始,*结束;
//指向字符串的开头
start=ch;
//在字符串中查找下一个空格
结束=strchr(开始“);
//而字符串中有更多的空格
while(end!=NULL)
{
//设置一个临时指针,从
//当前单词
char*p=结束;
//当不在当前单词的开头时,减小
//指针并打印它指向的字符
while(p--!=开始)
putchar(*p);
putchar(“”);
//查找下一个空格字符,从该字符开始
//在前一个空格字符之后。
开始=结束+1;
结束=strchr(开始“);
}
//我们没有找到另一个空格字符,这意味着我们正处于
/
char *ch = "krishna is the best";
char *buf = malloc(strlen(ch) + 1);
if (buf)
{
strcpy(buf, ch);
// reverse the contents of buf
}
#include <stdio.h>
#include <string.h>
int main(void)
{
char *ch = "krishna is the best";
char *start, *end;
// point to the beginning of the string
start = ch;
// find the next space in the string
end = strchr(start, ' ');
// while there are more spaces in the string
while (end != NULL)
{
// set up a temporary pointer, starting at the space following the
// current word
char *p = end;
// while aren't at the beginning of the current word, decrement the
// pointer and print the character it points to
while (p-- != start)
putchar(*p);
putchar(' ');
// find the next space character, starting at the character
// following the previous space character.
start = end + 1;
end = strchr(start, ' ');
}
// We didn't find another space character, meaning we're at the start of
// the last word in the string. We find the end by adding the length of the
// last word to the start pointer.
end = start + strlen(start);
// Work our way back to the start of the word, printing
// each character.
while (end-- != start)
putchar(*end);
putchar('\n');
fflush(stdout);
return 0;
}
Here is my working solution ->
#include<stdio.h>
#include<ctype.h>
#include<string.h>
char * reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/*Function to reverse words*/
char * reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */
return s;
}
int main(void)
{
char str[]="This is the test";
printf("\nOriginal String is -> %s",str);
printf("\nReverse Words \t -> %s",reverseWords(str));
return 0;
}
Here is my working solution ->
#include<stdio.h>
#include<ctype.h>
#include<string.h>
char * reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
}
/*Function to reverse words*/
char * reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == ' ')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */
return s;
}
int main(void)
{
char str[]="This is the test";
printf("\nOriginal String is -> %s",str);
printf("\nReverse Words \t -> %s",reverseWords(str));
return 0;
}