字符串处理
#include "strings.h"
#include
#include
static void printAllWords(char const* string, size_t numberofWords)
{
char buffer[256];
for (size_t i = 0; i < numberofWords; i++)
{
stringPrint(stringGetWord(string, buffer, i));
putchar(', ');
}
puts("");
}
int main() {
const char* strings[] = {
"Dollar#",
"One dollar#",
" Two dollars #",
" Three dollars here #",
"Four dollars in all #" };
char firstBuffer[8]; // Just able to hold the first string
char secondBuffer[16];
char thirdBuffer[32];
char fourthBuffer[512]; // big enough
char fifthBuffer[512];
stringCopy(strings[0], firstBuffer); stringPrint(firstBuffer); printf("\n");
stringCopy(strings[1], secondBuffer); stringPrint(secondBuffer); printf("\n");
stringCopy(strings[2], thirdBuffer); stringPrint(thirdBuffer); printf("\n");
stringCopy(strings[3], fourthBuffer); stringPrint(fourthBuffer); printf("\n");
stringCopy(strings[4], fifthBuffer); stringPrint(fifthBuffer); printf("\n");
printf("First: %zu == %zu\n", stringLength(strings[0]), stringLength(firstBuffer)); //某些编译系统下此处需进行修改,下同
printf("Second: %zu == %zu\n", stringLength(strings[1]), stringLength(secondBuffer));
printf("Third: %zu == %zu\n", stringLength(strings[2]), stringLength(thirdBuffer));
printf("Fourth: %zu == %zu\n", stringLength(strings[3]), stringLength(fourthBuffer));
printf("Fifth: %zu == %zu\n", stringLength(strings[4]), stringLength(fifthBuffer));
printf("-------\n");
printf("getWord tests\n");
// stringPrint(stringGetWord(strings[3], thirdBuffer, 2));
printAllWords(firstBuffer, 1);
printAllWords(secondBuffer, 2);
printAllWords(thirdBuffer, 2);
printAllWords(fourthBuffer, 3);
printAllWords(fifthBuffer, 4);
printf("-------\n");
/* printf("Checking what happens if a word can't be found\n");
char buf[1]; //某些编译系统下此处需要修改程序才能运行
buf[0] = '$';
char* ptr = stringGetWord(firstBuffer, buf, 1); //1.某些编译系统下此处需要修改程序才能运行; 2. 请深刻理解此函数、改变实参值并运行程序加深理解
if (ptr != NULL)
printf("Function did not return NULL...\n");
if (buf[0] != '$')
printf("Buf has been modified...\n");
*/
}
#include
#include
void stringPrint(const char* str)
{
const char* p=str;
for (; (*p) != '#'; p++)
printf("%c", *p);
}
void stringCopy(const char* str, char* dest)
{
int i;
const char* p1 = str;
char* p2 = dest;
for (i = 0; *(p1 + i) != '#'; i++)
*(p2 + i) = *(p1 + i);
*(p2 + i) = '#';
}
size_t stringLength(const char* str)
{
size_t i, num = 0;
const char* p = str;
for (i = 0; *(p + i) != '#'; i++)
num++;
return(num);
}
char* stringGetWord(const char* str, char* dest, size_t index)//
{
int num,word = 0;
char* p0 = dest;
/*for (num = 0; *str != '#'; str++)
{
if (*str == ' ')
word = 1;
else if (word == 0)
{
word = 1;
num++;
}
} //先算字符串单词个数用来判断
if (num == 0 || index > num - 1)
return NULL;
else {}*/
stringCopy(str, dest); //else后面这块对了!
for (num=0; num<= index; )
{
str++; dest++;
if (*str == ' ')
word = 0;
else if (word == 0)
{
word = 1;
num++;
p0 = dest; //把指向第index个的地址储存下来,因为待会dest要去寻找后面的空格或#,地址要变
}
}
for (; ( * dest != ' ') && (*dest != '#'); dest++);
*dest = '#';
dest = p0;
return(dest);
}/**/
#ifndef _C_STRINGS_H
#define _C_STRINGS_H
#include
/* All functions in this header operate on strings that use '#' as their terminating character, not \0.
* In addition, you can assume that all buffers are big enough (but not bigger!) for the operation. */
/* This function works just like strlen(), except the terminating character is #, not \0 */
size_t stringLength(const char* str);
/* This function works just like strcpy(), except the terminating character is #, not \0 and the parameters are in different order.
* (The function copies the string pointed to by str to the memory area pointed to by dest.)
* Oh, and unlike strcpy, this function return nothing. */
void stringCopy(const char* str, char* dest);
/* This function is used to select a certain word from a string.
* String is delimited by the space character ' ' into words. In other words, Word is a sequence of characters without any space characters.
* The first word has index 0.
* The function writes the selected word to the string pointed to by "dest".
* If the index is out of bounds (the string does not have enough words), then the function does not modify dest and the return value is NULL.
* If the operation was successful, "dest" pointer is returned (pointing to the first character of the word).
*/
char* stringGetWord(const char* str, char* dest, size_t index);//
/* This function prints the string to the standard output. The function does not print
* any additional characters besides those present in the string. */
void stringPrint(const char* str);
#endif
char* stringGetWord(const char* str, char* dest, size_t index)//
{
int word = 0; // 单词个数
int flag = 0; // 单词标记
int find = 0; // 是否找到
char* p0=dest;
for (; *str != '#' && *str != '\0'; str++)
{
if (*str == ' ')
{
if (find == 1) {
break;
} else {
flag = 0;
continue;
}
}
else
{
if (flag == 0)
{
flag = 1;
word += 1;
}
if (word == index + 1)
{
find = 1;
*p0 = *str;
p0++;
}
}
}
if (find == 1) {
*p0 = '#';
return dest;
} else {
return NULL;
}
}
修改如下
char* stringGetWord(const char* str, char* dest, size_t index)//
{
int count=0;
int len=stringLength(str);
int Left=0,Right=0;//左指针Left,右指针Right
for(Left=0; Left<len; Left++)
{
for(Right=0; Right<len-Left; Right++)
{
int fla=1;
for(int i=Left;i<=Left+Right;i++)
{
if(str[i]==' '||str[i]=='#')
{
fla=0;
}
}
if(str[Left-1]!=' '&&str[Left-1]!='#')
fla=0;
if(str[Left+Right+1]!=' '&&str[Left+Right+1]!='#')
fla=0;
//printf("fla%d ch:%c %c\n",fla,str[Left],str[Left+Right]);
if(fla==1)//判断从左指针到右指针,之间是不是一个单词
{
if(count==index)
{
int p=0;
for(int j=Left;j<=Left+Right;j++)
{
dest[p++]=str[j];
}
dest[p]='#';
}
count ++;
break;
}
}
}
return(dest);
}/**/
char* stringGetWord(const char* str, char* dest, size_t index)
{
int i, j, first = 0;
int flag = 0, start;
size_t word = 0; //单词个数索引,从0开始
for(i = 0; *(str + i) != '#'; i++)
{
if(first == 0 && *(str + i) == ' ')
continue; //过滤字符串前面的空格
first = 1;
if(*(str + i) == ' ' && flag == 1) //匹配到空格,表示一个单词已结束
{
flag = 0;
if(index == word) //当前单词与需要查找的单词索引一致
{
for(j = 0; j < i - start; j++)
*(dest + j) = *(str + start + j);
*(dest + j) = '\0';
break; //已找到单词,跳出循环
}
word++; //单词数累加
}
else if(flag == 0) //匹配到一个单词的起始位置,并保存在str中的起始位置
{
flag = 1;
start = i;
}
}
if(index > word) { //找不到目标索引单词
return NULL;
}
if(flag == 1 && index == word) //最后一个单词是需要查找的单词
{
flag = 0;
for(j = 0; j < i - start; j++)
*(dest + j) = *(str + start + j);
*(dest + j) = '\0';
}
return (char *)(str + start);
}