字符串处理
#include "strings.h"
#include <stdio.h>
#include <stdlib.h>
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<stdio.h>
#include <stdlib.h>
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 <stdlib.h>
/* 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