简体   繁体   English

如何在不使用任何基本功能的情况下拆分字符数组

[英]How to split an array of characters without using any basic function

I have this function: int split(char* str, char s) , so how to split the str without using strtok() or other functions? 我有这个函数: int split(char* str, char s) ,那么如何在不使用strtok()或其他函数的情况下拆分str

Eg: str = "1,2,3,4,5", s = ',' 例如: str = "1,2,3,4,5", s = ','

After split(str, s) , output will be: split(str, s) ,输出将是:

1
2
3
4
5

Sorry guys, the int return -1 if str == NULL and return 1 if str != NULL. 抱歉,如果str == NULL,则int返回-1,如果str!= NULL,则返回1。

How about this? 这个怎么样? I'm not sure what the int return type means in the function so I made it the count of splits. 我不确定int返回类型在函数中的含义,因此我将其作为拆分的计数。

#include <stdio.h>
int split(char* str, char s) {
    int count = 0;
    while (*str) {
        if (s == *str) {
            putchar('\n');
            count++;
        } else {
            putchar(*str);
        }
        str++;
    }
    return count;
}

I didn't write code for years, but that should do? 我好多年没写代码了,但是应该这样做吗?

while (*str) // as long as there are more chars coming...
{
  if (*str == s) printf('\n');  // if it is a separator, print newline
  else printf('%c',*str);       // else print the char
  str++;     // next char
}
string split(const char* str, char s) {
    std::string result = str;
    std::replace(result.begin(), result.end(), s, '\n');
    result.push_back('\n'); // if you want a trailing newline
    return result;
}

Another approach... 另一种方法

#include <iostream>
using namespace std;

void split(char* str, char s){
    while(*str){
        if(*str==s){
            cout << endl;
        }else{
            cout << *str;
        }
        str++;
    }
    cout << endl;
}

int main(){

   split((char*)"herp,derp",',');
}

and another one with iterator 另一个带有迭代器

#include <iostream>
using namespace std;



int main() {
    string s="1,2,3,4,5";
    char   cl=',';
    for(string::iterator it=s.begin();it!=s.end();++it) 
      if (*it == cl) 
        cout << endl; 
      else cout << *it;

    return 0;
}

http://ideone.com/RPrls7 http://ideone.com/RPrls7

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我需要一个函数来从 C++ 中的 char 数组中删除某些字符而不使用任何索引 - I need a function to delete certain characters from a char array in c++ without using any index 如何在不使用数组或任何库函数(用于反转的任何函数)的情况下反转用户输入? - How to reverse a user input without using array or any library function(any function for reversing)? 如何在C ++中将字符串拆分为字符数组? - How to split a string to an array of characters in C++? c++ 不使用任何库拆分字符数组 - c++ Split char array without use of any library 如何使用PlaySound()或任何基本功能/方法同时在C ++中播放超过1 .wav声音? - How to play more than 1 .wav sounds in C++ at the same time using PlaySound() or any basic function/method? 如何使用字符串拆分数组? - How to split an array using strings? 如何在没有任何临时char数组作为变量的情况下将字符数组传递给C ++中的函数? - How to pass character array to a function in C++ without any temporary char array as variable? 有没有办法在不使用任何迭代的情况下按字母顺序排列字符串中的字符? - Is there a way to alphabetize the characters in a string without using any iteration? 在不使用任何String函数的情况下读取Character数组中的字符串,甚至不使用Java中的charAt - Reading a string in Character array without using any String function, not even charAt in Java 在不使用任何分号的情况下编写 function - Write a function without using any semicolon
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM