C 使用指针反转字符串中的单词
有人问了我一个分为两部分的问题。第一部分是通过字符串操作反转字符串中的单词,为此我使用strcpy和strcat。然而,对于B部分,我必须使用指针反转单词。现在我有下面显示的代码。我走对了吗 我的函数背后的思想是,我有原始字符串C 使用指针反转字符串中的单词,c,string,algorithm,pointers,C,String,Algorithm,Pointers,有人问了我一个分为两部分的问题。第一部分是通过字符串操作反转字符串中的单词,为此我使用strcpy和strcat。然而,对于B部分,我必须使用指针反转单词。现在我有下面显示的代码。我走对了吗 我的函数背后的思想是,我有原始字符串string1,我有一个指向起始字符的指针,然后在字符串中迭代,直到我找到一个空格,给出单词的大小。然后我把这个词放在新字符串的末尾 代码: partb(字符*string1,int-s) { int i=0,j=0,k=0,count=0; char temp[100]
string1
,我有一个指向起始字符的指针,然后在字符串中迭代,直到我找到一个空格,给出单词的大小。然后我把这个词放在新字符串的末尾
代码:
partb(字符*string1,int-s)
{
int i=0,j=0,k=0,count=0;
char temp[100]={0},*sp=string1;
对于(i=0;i一些观察:
- 您如何知道
temp
将足够大以存储反向字符串?您应该分配一个与输入字符串大小相同的char*
- 当你知道isalnum(string1[i])
为假时,为什么要测试string1[i]=''
?你已经处于分词状态,因此测试是不必要的
count
初始化为0。每次遇到生词时,必须重置count
a
和b
,以相反方向遍历单词,而不是使用count
该计划展示了我的方法:
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
int isWordCharacter(char c) {
return isalnum(c);
}
char * reverseWords(char *s) {
int pos = 0, seek, a, b, len;
for (len = 0; s[len] != 0; ++len) { // Find the string length ourselves to
} // avoid using anything from string.h.
char *result = malloc(len * sizeof(char));
while (1) {
while (!isWordCharacter(s[pos])) { // Look for the start of a word.
result[pos] = s[pos];
if (pos == len) { // Are we at the end of the string?
return result;
}
++pos;
}
for (seek = pos + 1; isWordCharacter(s[seek]); ++seek) {
} // Look for the end of the word.
for (a = pos, b = seek - 1; a < seek; ++a, --b) {
result[b] = s[a]; // Scan the word in both directions.
}
pos = seek; // Jump to the end of the word.
}
}
int main() {
char *s = "Hello, world. Today is September 20, 2015.";
printf("%s\n%s\n", s, reverseWords(s));
}
#包括
#包括
#包括
int-isWordCharacter(字符c){
返回靛蓝(c);
}
char*reverseWords(char*s){
int pos=0,寻道,a,b,len;
对于(len=0;s[len]!=0;++len){//找到要使用的字符串长度
}//避免使用string.h中的任何内容。
char*result=malloc(len*sizeof(char));
而(1){
而(!isWordCharacter(s[pos]){//查找单词的开头。
结果[pos]=s[pos];
如果(pos==len){//我们在字符串的末尾吗?
返回结果;
}
++pos;
}
for(seek=pos+1;isWordCharacter(s[seek]);+seek){
}//查找单词的结尾。
对于(a=pos,b=seek-1;a
一直走到撞到墙上,然后回来询问如何继续。“a部分……我使用了strcpy
和strcat
”是否不允许使用指针算法定义您的strcpy
和strcat
版本?换句话说,是否允许将B部分缩减为A部分?@AndrewRicci感谢您接受我的回答。您是否介意对其进行投票?您可以使用库
,然后使用函数isalnum(字符c)
做同样的事情,比如isWordCharacter(charc)
很好。我将在isWordCharacter
中这样做,以保持它的通用性。
#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>
int isWordCharacter(char c) {
return isalnum(c);
}
char * reverseWords(char *s) {
int pos = 0, seek, a, b, len;
for (len = 0; s[len] != 0; ++len) { // Find the string length ourselves to
} // avoid using anything from string.h.
char *result = malloc(len * sizeof(char));
while (1) {
while (!isWordCharacter(s[pos])) { // Look for the start of a word.
result[pos] = s[pos];
if (pos == len) { // Are we at the end of the string?
return result;
}
++pos;
}
for (seek = pos + 1; isWordCharacter(s[seek]); ++seek) {
} // Look for the end of the word.
for (a = pos, b = seek - 1; a < seek; ++a, --b) {
result[b] = s[a]; // Scan the word in both directions.
}
pos = seek; // Jump to the end of the word.
}
}
int main() {
char *s = "Hello, world. Today is September 20, 2015.";
printf("%s\n%s\n", s, reverseWords(s));
}