如何在C不使用的strtok分割字符串 [英] How to split string in C without using strtok

查看:327
本文介绍了如何在C不使用的strtok分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<&stdio.h中GT;
INT
主(){
    字符字符串[] =我的名字是geany
    INT长度= sizeof的(串)/的sizeof(字符);
    的printf(%i的,长度);
    INT I;
    对于(i = 0; I<长度;我++){    }
    返回0;
}

如果我想打印我的名称是和geany分开那么我该怎么办。我想用一个delimnator,但我不知道该怎么做在C


解决方案

  1. 开始一个指向字符串的开头

  2. 按字符迭代字符,寻找你的分隔符

  3. 每次你找到一个时间,你必须从不同的长度的最后一个位置字符串 - 你想什么

  4. 设置新的开始位置的分隔符+ 1,转到步骤2。

做所有这些,同时还有剩余字符串中的字符...

#include <stdio.h>
int
main() {
    char string[] = "my name is geany";
    int length = sizeof(string)/sizeof(char);
    printf("%i", length);
    int i;
    for ( i = 0; i<length; i++ ) {

    }   
    return 0;
}

if i want to print "my" "name" "is" and "geany" separate then what do I do. I was thinking to use a delimnator but i dont know how to do it in C

解决方案

  1. start with a pointer to the begining of the string
  2. iterate character by character, looking for your delimiter
  3. each time you find one, you have a string from the last position of the length in difference - do what you want with that
  4. set the new start position to the delimiter + 1, and the go to step 2.

Do all these while there are characters remaining in the string...

这篇关于如何在C不使用的strtok分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆