热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

C将字符串解析为没有任何lib函数的字符数组-Cparsingstringtoarrayofcharswithoutanylibfunctions

WhatIamtryingtodo,isparsestring,containingspacestoarrayofarraysofchars.Soifinput

What I am trying to do, is parse string, containing spaces to array of arrays of chars. So if input was "ab cd ef", I want my array to be array[0]==ab, array[1]==cd,array[2]==ef. I hope that that makes it clear. Problem however is that i cant use any lib functions. No string.h, no fget, just scanf and printf. Here is snipplet of my code:

我正在尝试做的是解析字符串,包含字符数组数组的空格。因此,如果输入是“ab cd ef”,我希望我的数组是数组[0] == ab,数组[1] == cd,数组[2] == ef。我希望这说清楚。但问题是我无法使用任何lib函数。没有string.h,没有fget,只有scanf和printf。这是我的代码的snipplet:

char word[25] = "";
char *words[25];

for (int i = 0; i <= find_length(input); i++){
    if(input[i] == ' ' || i == find_length(input)){
        words[position] = word;
        printf("%s%d ",words[position], position);
        position++;
        counter = 0;
        word[0] = '\0';
    }
    else{
        word[counter] = input[i];
        counter++;
    }
}

position = 0;

while(counter <5){
    printf("%s%d ",words[position], position);
    counter++;
}

First I parse string into individual words and then I try to put them into array. I hope that my logic is correct. So the problem is, that first printf (one in for loop) prints proper values and their positions. However second printf (which is only to make sure that string was parsed properly) prints just 0. So for example input "ab cd ef" gives:

首先,我将字符串解析为单个单词,然后我尝试将它们放入数组中。我希望我的逻辑是正确的。所以问题是,第一个printf(for循环中的一个)打印正确的值及其位置。然而,第二个printf(仅用于确保正确解析字符串)仅打印0.因此,例如输入“ab cd ef”给出:

ab0 cd1 ef2 0 0 0 0 0

ab0 cd1 ef2 0 0 0 0 0

I suspect that problem is that I am not adding values to the array of arrays, just assigning pointers. And since I dump "word", they point to nothing. If that is correct, how do I assign values instead of pointers?

我怀疑问题是我没有为数组数组添加值,只是分配指针。因为我倾倒“字”,他们指向什么都没有。如果这是正确的,我如何分配值而不是指针?

Of course this assumption might be wrong, if so could you point me to my mistake and how to fix it? Thank you.

当然这个假设可能是错误的,如果是这样,你能否指出我的错误以及如何解决它?谢谢。

PS: I know that I should strcpy value of string not = it, but as I said, I cant use any lib functions other than scanf and printf (find_length is my own function)

PS:我知道我应该strcpy string的值not = it,但正如我所说,我不能使用除scanf和printf之外的任何lib函数(find_length是我自己的函数)

2 个解决方案

#1


1  

I don't really agree with @harper 's answer because the main problem is the following, as the OP almost figured out himself:

我真的不同意@harper的回答,因为主要的问题如下,因为OP几乎想出了自己:

I suspect that problem is that I am not adding values to the array of arrays, just assigning pointers. And since I dump "word", they point to nothing. If that is correct, how do I assign values instead of pointers?

我怀疑问题是我没有为数组数组添加值,只是分配指针。因为我倾倒“字”,他们指向什么都没有。如果这是正确的,我如何分配值而不是指针?

This code will make words[0] point to the block of memory associated with the char array word. Then words[1] will point to that same block. Then words[3] and so on..

此代码将使单词[0]指向与char数组字相关联的内存块。然后单词[1]将指向同一个块。然后单词[3]等等..

In the end, all elements inside the char* array words[] will point to the char array word[], which will contain a '\0' as its first element, seeing that the OP does this word[0] = '\0'; repeatedly.

最后,char * array words []中的所有元素都将指向char数组word [],它将包含一个'\ 0'作为其第一个元素,看到OP执行此单词[0] ='\ 0' ;反复。

So my proposed fix, besides what's already mentioned about the while loop at end, would be to allocate memory for each substring and then copy the contents of word[] with a simple for loop.

所以我提出的修复,除了已经提到的关于while循环的内容之外,将为每个子字符串分配内存,然后用简单的for循环复制word []的内容。

So this:

所以这:

for (int i = 0; i <= find_length(input); i++){
    if(input[i] == ' ' || i == find_length(input)){
        words[position] = word;
        printf("%s%d ",words[position], position);
        position++;
        counter = 0;
        word[0] = '\0';
    }

Would become this:

会变成这样的:

for (int i = 0; i <= find_length(input); i++) {
    if(input[i] == ' ' || i == find_length(input)) {
    words[position] = malloc(sizeof(char) * (counter + 1));
        for (int j = 0; j 

#2


1  

Replace the last lines with this snippet:

用此代码段替换最后一行:

position = 0;

while(position <5) {
    printf("%s%d ", words[position], position);
    position++;
}

推荐阅读
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • [大整数乘法] java代码实现
    本文介绍了使用java代码实现大整数乘法的过程,同时也涉及到大整数加法和大整数减法的计算方法。通过分治算法来提高计算效率,并对算法的时间复杂度进行了研究。详细代码实现请参考文章链接。 ... [详细]
  • 本文介绍了使用哈夫曼树实现文件压缩和解压的方法。首先对数据结构课程设计中的代码进行了分析,包括使用时间调用、常量定义和统计文件中各个字符时相关的结构体。然后讨论了哈夫曼树的实现原理和算法。最后介绍了文件压缩和解压的具体步骤,包括字符统计、构建哈夫曼树、生成编码表、编码和解码过程。通过实例演示了文件压缩和解压的效果。本文的内容对于理解哈夫曼树的实现原理和应用具有一定的参考价值。 ... [详细]
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文介绍了如何在给定的有序字符序列中插入新字符,并保持序列的有序性。通过示例代码演示了插入过程,以及插入后的字符序列。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • 题目描述:一个DNA序列由ACGT四个字母的排列组合组成。G和C的比例(定义为GC-Ratio)是序列中G和C两个字母的总的出现次数除以总的字母数目(也就是序列长度)。在基因工程中,这个 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 本文介绍了P1651题目的描述和要求,以及计算能搭建的塔的最大高度的方法。通过动态规划和状压技术,将问题转化为求解差值的问题,并定义了相应的状态。最终得出了计算最大高度的解法。 ... [详细]
  • 猜字母游戏
    猜字母游戏猜字母游戏——设计数据结构猜字母游戏——设计程序结构猜字母游戏——实现字母生成方法猜字母游戏——实现字母检测方法猜字母游戏——实现主方法1猜字母游戏——设计数据结构1.1 ... [详细]
  • Linux环境变量函数getenv、putenv、setenv和unsetenv详解
    本文详细解释了Linux中的环境变量函数getenv、putenv、setenv和unsetenv的用法和功能。通过使用这些函数,可以获取、设置和删除环境变量的值。同时给出了相应的函数原型、参数说明和返回值。通过示例代码演示了如何使用getenv函数获取环境变量的值,并打印出来。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • C语言常量与变量的深入理解及其影响
    本文深入讲解了C语言中常量与变量的概念及其深入实质,强调了对常量和变量的理解对于学习指针等后续内容的重要性。详细介绍了常量的分类和特点,以及变量的定义和分类。同时指出了常量和变量在程序中的作用及其对内存空间的影响,类似于const关键字的只读属性。此外,还提及了常量和变量在实际应用中可能出现的问题,如段错误和野指针。 ... [详细]
author-avatar
好人森森_195
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有