m0_68317398 2022-11-17 23:40 采纳率: 100%
浏览 65
已结题

c字符串处理重新发布血亏15块

字符串处理

img

#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



  • 写回答

3条回答 默认 最新

  • togolife 2022-11-18 00:16
    关注
    
    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 11月26日
  • 已采纳回答 11月18日
  • 创建了问题 11月17日

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度