作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
*有人可以帮我实现这个功能吗?我试图将字符串输入分成标记,并将每个标记移动一些指定的量。 *
char *tokenize(char *f, int shift){
const char delim[] = " .;\n\t";
char *pa = f; //points to the beginning of *f
//size of *f
int i;
int stringSize = 0;
for(i = 0; f[i] != '\0'; i++)
{
stringSize++;
}
//put string in array to pass to strtok function
char newString[stringSize];
int j;
for(j = 0; j < stringSize; j++)
{
newString[j] = *f;
f++;
}
//break the words up into sub-strings without the delimiters
char *word = strtok(newString, delim);
while(word != NULL)
{
word = strtok(NULL, delim);
word = stringShift(word, shift);
//printf("After being shifted %d = %s\n", shift, word);
}
return pa;
}
/*Shift Function*/
char *stringShift(char *s, int k){
int i;
for(i = 0; s[i] != '\0'; i++)
{
s[i] += k;
}
return s;
}
最佳答案
据我所知,我认为这应该可以达到目的
char* addWordToArr(char *arr,char *word)
{
int i;
for(i =0;i<strlen(word);i++)
{
*arr++ = word[i];
}
*arr++ = ' ';
return arr;
}
char *tokenize(char *f, int shift){
const char delim[] = " .;\n\t";
int stringSize = strlen(f);
//put string in array to pass to strtok function
char newString[stringSize+1];
int j;
for(j = 0; j < stringSize; j++)
{
newString[j] = *f;
f++;
}
newString[stringSize] = '\0'; //null terminate
char *rVal = malloc(sizeof(char) * (stringSize +1)); //The total length of the tokenized string must be <= the original string
char *writePtr = rVal;
//break the words up into sub-strings without the delimiters
char *word = strtok(newString, delim);
word = stringShift(word, shift);
writePtr = addWordToArr(writePtr,word);
while(word != NULL)
{
word = strtok(NULL, delim);
if(word)
{
word = stringShift(word, shift);
writePtr = addWordToArr(writePtr,word);
}
}
writePtr = '\0';
return rVal;
}
产生:
string before shift bish;bash;bosh hyena trout llama exquisite underwater dinosaur
string after shift dkuj dcuj dquj j{gpc vtqwv nncoc gzswkukvg wpfgtycvgt fkpqucwt
stringShift 函数不变
关于c - 将字符串拆分为标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22824356/
我是一名优秀的程序员,十分优秀!