由买买提看人间百态

topics

全部话题 - 话题: strtok
1 (共1页)
p******s
发帖数: 938
1
来自主题: Unix版 - [转载] UNIX下的strtok
【 以下文字转载自 Programming 讨论区 】
【 原文由 phageous 所发表 】
要利用strtok来进行一些string的操作,结果却是segmentation fault,
code如下:
#include
#include
void main() {
char *str;
char *line="15:wildwood.eecs.umich.edu:018032:24.79 wildwood.eecs.umich.edu:018031:21.11 wildwood.eecs.umich.edu:018044:14.83 wildwood.eecs.umich.edu:018093:4.32";
str=strtok(line, ":");
printf("First token is %s\n", str);
while( (str=strtok(NULL, ":")) !=NULL)
printf("Next token is %s \n", str
s*********e
发帖数: 17
2
来自主题: Programming版 - 如何实现 strtok() ?
char * strtok( const char * string, const char * delimiters );
Sequentially truncate string if delimiter is found.
第一部分:
If string is not NULL, the function scans string for the first occurrence of
any character included in delimiters. If it is found, the function overwrites
the delimiter in string by a null-character and returns a pointer to the token
, i.e. the part of the scanned string previous to the delimiter.
第二部分:
After a first call to strtok, the function may be called with NULL as str
c****d
发帖数: 116
3
来自主题: Unix版 - [转载] UNIX下的strtok
Please change the line:
char *line=.....
to
char line[1024]=....
to see what's the problem.

【 以下文字转载自 Programming 讨论区 】
【 原文由 phageous 所发表 】
要利用strtok来进行一些string的操作,结果却是segmentation fault,
code如下:
#include
#include
void main() {
char *str;
char *line="15:wildwood.eecs.umich.edu:018032:24.79 wildwood.eecs.umich.edu:018031:21.11 wildwood.eecs.umich.edu:018044:14.83 wildwood.eecs.umich.edu:018093:4.32";
str=strtok(line, ":");
printf("First token is %
c*******h
发帖数: 1096
4
来自主题: Programming版 - 有没有类似strtok的函数
不过不是对string用的,而是对file stream用的
getdelim只能有一个固定的delimiter,不够用啊。想要像strtok那样
可以指定一堆delimiter的。有这样的函数不?
k****f
发帖数: 3794
5
FILE*fp=fopen("youdata","r");
char buf[1000];
int first,second;
char third[10],fourth[10];
double fifth;
while(fgets(buf,sizeof(buf),fp)){
char*p=strtok(buf,"|");
if(p==NULL)break;
first=atoi(p);
p=strtok(NULL,"|");
second=atoi(p);
p=strtok(NULL,"|");
strcpy(third,p);
p=strtok(NULL,"|");
strcpy(fourth,p);
p=strtok(NULL,"|");
fifth=atof(p);
}
fclose(fp);

how
.
me
r*******y
发帖数: 290
6
来自主题: JobHunting版 - 说说某著名软件公司的onsite面试
公司就不说了
面试一共见了4个人,都是上来就白板写程序,每人一道问题
1.写 char* strtok(char* str, const char* delimeter) code.
用法为:第一次调用 char* p = strtok(string, delimeter);
以后调用 p=strtok(NULL, delimeter);
关键:函数内部用static char* temp存储第一次pass的string
2.美国的coin设计为1,5,10,25,任意给定一个change,
用greedy algorithm可以算出最少所需要的coins,设计一套coins,
证明greedy algorithm给的solution不是最少的
然后在写一个algorithm,给定四个coin值,找出最少的coins数
数学上来说,min x1+x2+x3+x4 st. ax1+bx2+cx3+dx4=change
可以用DP
3. 给定5张牌,写一个函数判断是否是two pairs
4. 给定400个array,每个array有若干integers,找出这些array的
合集,交集,
s*****s
发帖数: 157
7
来自主题: JobHunting版 - 继续攒人品 报几家面经
ip address那个, 用strtok好像容易些, 下面的code能否完成:
int foo(char *IP)
{
if(strlen(IP) > 16)
return false;
if(strlen(IP) < 8)
return false;
for (int i = 0 ; i < strlen(IP); i++)
{
if (!(IP[i] >= '0' && IP[i] <= '9' || IP[i] == '.'))
return false;
}
char * range = strtok(IP, ".");
short c = 0;
while ((range != NULL) && (c < 4))
{
if((strlen(range) <= 3) && (atoi(range) >= 0 &&(atoi(range) <= 255))
++c;
else
break;

range... 阅读全帖
n*p
发帖数: 298
8
来自主题: CS版 - 一个程序的小问题
我是在用fread读一个文本文件到buffer里,
然后用strtok(file_buffer,DELIMITERS)来parse,
这个文本文件只有10个字符,两个单词
可parse之后总是会多几个怪字符怎么办?
好像strtok没看到buffer(也就是file)的end
N***m
发帖数: 4460
9
not sure what you are asking for. perhaps strtok function can do this kind
of job. http://www.cplusplus.com/reference/clibrary/cstring/strtok/
p**o
发帖数: 3409
10
手写了一些C扩展,有些返回多重指针的函数不知道怎么用SWIG来包来供Python调用……
比如下面这个strsplit()函数,返回的是char**,怎么改才能让Python收到一个list (
of strings)?
http://www.swig.org/tutorial.html
我只是照tutorial简单地把函数声明抄进.i文件,Python中调用时返回的是

#include
#include
#include
/* Split an input string 'instr', using a set of given delimiters, to an
array of strings of at most 'maxparts' parts. */
char **strsplit (const char *instr, const char *delimiters, size_t maxparts)
{
char *... 阅读全帖
f**********d
发帖数: 4960
11
来自主题: Programming版 - c的问题
试了报错,其实我是要逐行读入文件,并把每一行用";"分隔成不同fields,再把
fields存入array里。
文件第一行是header,之后每行为 v1;v2;v3;v4;v5
现在*Words[j] = *pch;报错了,说access violation writing location 0xccccccccc.
fp = fopen("Data Files\ChestClinic.txt", "r");
if (fp == NULL)
{
printf("file is null");
}
i = 0;
while (fgets(line, sizeof(line), fp))
{
if (i == 0)
{
printf("The format of network structure file is:n");
printf("%sn", line);
... 阅读全帖
s***n
发帖数: 442
12
明天第一个onsite, 东海岸小公司,CTO直接电面然后安排了onsite. 凌晨3,4点就要到
机场,晚上12点才能回.公司到机场还有一个小时车程, 对体力是极大的考验阿!!
后边还有几个比较心仪的大公司,电面或者onsite, 这次就放松心情,努力争取了..争取
先弄一个保底的.呼呼
攒人品,求bless. 发之前一位前辈总结的面试知识点. 个人认为这些知识点+resume就
概括了基本的技术问题了.
C: pointer, call by value/pointer, return the pointer of a local variable,
string manipulations, source code of some important C string subroutines
(strcpy, strtok, etc), itoa, atoi, static variable and fuction, name
mangling,
memory allocation
http://www.eskimo.com/~scs/C-faq/faq.html
C++: na
s*******e
发帖数: 27
13
来自主题: JobHunting版 - 问几个multithreading的问题
There are some details related to multithreaded issues.
If each thread has its own signal handling procedure, although the process
does send the signal to the correct thread, there might be some race
conditions in handling the signal. One solution is to dedicate one thread
just to handle the signals for all threads and decides which thread to call
(wait up).
The other issue is that some functions like string token function, strtok,
uses a static variable to implement its recursive calls. So if m
o**********t
发帖数: 406
14
来自主题: JobHunting版 - 记得一年多前失败的电面
web white board coding, 问题是写一个类似 strtok 这样的函数。
看上去很简单,三下五除二就写好了。结果马上被问得很狼狈:
Q: 你咋知道空格是用来分隔单词的捏?
Q: 如果有多个不同的分隔符咋办捏?
Q: 如果是双字节语言?
Q: 如果是从右往左的语言捏?
当场就死菜了 ...
提醒大家,多问要求,把方方面面都想好了再动手 ...
z********y
发帖数: 109
15
来自主题: JobHunting版 - 请问FlexTrade这个公司怎么样?
我也面了这个公司,在CT,一个月了,猎头让我接着等。
里面老印很多,HR的女士老的老,丑的丑,所以这家公司应该比较nice,雇了这么多上
不了台面的。我进了屋,就想走了。
做的东西不错,trading platform,可以学不少。给办H1B。
问题:
strtok
蛋糕3刀分8块
revserse double linked list (recursive and non-recursive)
write a singleton
还要做套题
...
v******a
发帖数: 54
16
来自主题: JobHunting版 - flextrade面经
c++ developer
两轮电面c++
onsite:
笔试: 30 minute
careercup都有
有一个是
int fun(int **p);
a[3][3];
fun(a);
what's the problem
上机编程3题:2-3 hour
1. Implement a binary tree. Insert following strings in the tree.
"Acura" "Aston Martin" " Bentley" "BMW"..."Ford" "Ferrari".
Search for "Cadillac" in the tree.
2. Implement code to evaluate string "5+4*5-8-9+6*5-6/3+2"
3. Write quick sort method for sorting integers.
Then Talk with several persons:
Most problems come from careercup.
Here are remembered questions.
1.... 阅读全帖
m*****y
发帖数: 93
17
C比C++简单,把K&R C和The C Library Reference Guide看完就可以了。很多标准库函
数的细节问题,比如strtok之类的。
h****e
发帖数: 928
18
来自主题: JobHunting版 - 不想做题了咋办呢?
你还是要这儿继续多灌水,大家做题才有乐趣,否则太枯燥了。
LeetCode上的题目你大概是用Java做的吧,那么就再用C++做一遍。做完了
再用Javascript、PHP、Python...
我还想出LeetCode上的一些题目的扩展。例如,做了字符串的加法和
乘法,就再做字符串的减法和除法。做了Integer to Roman,就再做
Roman to Integer。做了strstr,就再做strtok, strspn, strcspn等等。
n*****g
发帖数: 178
19
本帖内容来自于:
发信人: segin (segin), 信区: JobHunting
标 题: 明天ONSITE攒人品,发面试知识点总结!!
发信站: BBS 未名空间站 (Thu Feb 4 13:40:29 2010, 美东)
http://www.mitbbs.com/bbsann2/life.faq/JobHunting/17/D128425435
非常好的总结啊!!!!攒人品再转发一次。
C: pointer, call by value/pointer, return the pointer of a local variable,
string manipulations, source code of some important C string subroutines
(strcpy, strtok, etc), itoa, atoi, static variable and fuction, name
mangling,
memory allocation
http://www.eskimo.com/~scs/C-faq/faq.html
C++: name... 阅读全帖
n*******e
发帖数: 612
20
来自主题: JobHunting版 - 上一道我以前喜欢出的题目吧
c++ 用strtok在atoi吧
或者sscanf循环
y******6
发帖数: 49
21
来自主题: JobHunting版 - 报个amazon summer intern面经,求bless

第一道题可以直接用 C++里的strtok吗?还是需要自己写函数实现 from scratch
r*****e
发帖数: 792
22
来自主题: JobHunting版 - 上个啰嗦的2西格玛失败面经吧
recruiter找上门来,问我去nyc financial industry怎么样?我说好啊,只要给钱多
。其实不想离开湾区,觉得nyc就跟北京一样,还破旧,没什么意思,另外搬到东边拖
家带口的也够麻烦的。但是因为的确打算跳槽,所以热热身也挺好,至少能
让自己保持状态。
先和HR的人聊了一次,20分钟吧。然后就是online code test, 2 problems in 2
hours,题可以从网上找到,仔细点就好了。接着1个小时的phone interview,问了
很多问题,数据结构的,算法的,编程的一些细节,os的,有些问题看看150里面有,
尤其是os和系统的问题, process vs thread, sizeof(many data types) in
different OS(32vs64), binary search,分析复杂度。过了就安排onsite了。
因为看到网上说表现不好半道就请出的故事,还是好好准备了一下,以免过早被
轰走丢脸。看了玻璃门和bbs上的面经,觉得还挺难,而且非常杂,外加猎头给的一些
题,所以准备过程还是挺累人的。除了算法外,还复习了概率,... 阅读全帖
r**h
发帖数: 1288
23
如果不需要正则表达式的话,前者可以用C里面的strtok,以及stl里面的reverse
s*********s
发帖数: 318
24
来自主题: JobHunting版 - 发T家面经
C++ static? like sth in strtok()?
f***c
发帖数: 338
25
来自主题: JobHunting版 - reverse words in a string
In Python:
def reverseWord(s):
return ' '.join([x[-1::-1] for x in s.split()])
^_^ (Sorry Python is not good for alg. demonstration but good for solution)
In C++:
if we can use strtok lib function, then we get each word from the string.
next for each word do the reverse
Is it good? waiting for comments.

letter
k****f
发帖数: 3794
26
来自主题: Programming版 - A function can be history-sensitive??????
strtok
应该是说这次调用的结果和上一次的参数有关的函数。
d******g
发帖数: 27
27
来自主题: Programming版 - C++ read matrix from txt file
能先用 getline, 再用strtok么?
f******n
发帖数: 90
28
来自主题: Programming版 - C库函数的实现
请问哪里可以找到C库函数的实现,例如strcmp, strtok等等。 在linux上一般存在哪
个目录?
p**v
发帖数: 853
29
用strtok()把45和568定位出来,并得到各自字符长度,
找个stringify()将1.095换成string.c_str(),
再组合起来。
用char挺麻烦的,要用好几次strcpy,strcat,strlen
p***o
发帖数: 1252
30
来自主题: Programming版 - C++读文件的一个基础问题
fgets+strtok
k****f
发帖数: 3794
31
来自主题: Programming版 - 求教:取串中的子串好方法
strtok(str,", ");
l***e
发帖数: 480
32
来自主题: Programming版 - 求教:取串中的子串好方法
thanx,
我用的是STRING对象。
strtok好像是字符指针的函数。
试试再说。
t*i
发帖数: 72
33
来自主题: Programming版 - 一个c语言的问题
非常感谢, 刚才查了一下似乎strtok()也可以用, 不知道那种效率比较高一些,谢谢。

s3
s*********x
发帖数: 1923
34
来自主题: Programming版 - C++ 读不规则长度文件问题
c++高手帮帮忙,有一个文件
A01 B01 (13,30,50) (20, 40, 60)
A02 B02 (13,30,50, 100, 200) (20, 40, 60, 105, 206)
我需要读出每行中的每一pair, 比如 (13, 20), (30, 40), (50, 60), 可问题是在第
三列和第四列中,我们不知道有几个数字,比如第一行中有3个pair,第二行中有4个
pair.
我现在的方法是定义四个string, a, b, c, d
fstream >> a >> b >> c >> d. then, c = (13,30,50), c++中怎么把这三个数字读出
来(比如像perl里的split function)?谢了。
Update: I solved the problem. Hope it can help others:
inputFile >> tmp >> tmp >> start >> end ;
....
num = strtok (start, ",");
while (num != NULL)
k****f
发帖数: 3794
35
来自主题: Programming版 - C++ 读不规则长度文件问题
strtok
r****o
发帖数: 1950
36
来自主题: Programming版 - 一个c语言读文件的问题
use strtok() to seperate the string
e******r
发帖数: 220
37
来自主题: Programming版 - a C language question
补充一下, 每十个数都差不多是下面的样式
206440.008 47.98979950 16.27552498 260.678058 0.0 74.40 0.14 0.37 -1.10 0.02
好象用strtok就可以把十个数parse出来, 可是如何检测每个字符串是float呢? 难道用atof()吗?
k****f
发帖数: 3794
38
来自主题: Programming版 - help on string parse
试试strtok

street
j**7
发帖数: 771
39
来自主题: Programming版 - 请教一个c语言实现多线程的问题
嗯。
不过我这些个并行的线程要调用自己写的一个比较大的函数,而且要调用很多次,倒没
有标准库里的函数,这样的话是不是就一定不会泄露呢??
查到一个说法:
如果在除主线程之外的任何线程中进行一下操作,你就应该使用多线程版本的C
runtime library,并使用_beginthreadex和_endthreadex:
1 使用malloc()和free(),或是new和delete
2 使用stdio.h或io.h里面声明的任何函数
3 使用浮点变量或浮点运算函数
4 调用任何一个使用了静态缓冲区的runtime函数,比如:asctime(),strtok()或rand()
这么说来基本啥都不能用了。beginthread能用waitForSingleObject来等待么?
k****f
发帖数: 3794
40
来自主题: Programming版 - 问个很基础的问题:C++的string
c下面,我一般是用strtok搞定分割的
b****j
发帖数: 78
41
来自主题: Programming版 - string /File IO processing using C
1. strtok
2. strncmp
c*****t
发帖数: 1879
42
来自主题: Programming版 - string /File IO processing using C
avoid using strtok at all costs. strchr is a better alternative.
scanf technically can scan with delimiter, but usually it is
faster to scan the whole line, then use strchr.
M**u
发帖数: 10158
43
来自主题: Programming版 - 有没有类似strtok的函数
对string stream的,这样的函数都没有把。。。
t****t
发帖数: 6806
44
来自主题: Programming版 - 有没有类似strtok的函数
generate istream_iterator and use find_first_of() in . depending
on your requirement, it may or may not be perfect.
t****t
发帖数: 6806
45
来自主题: Programming版 - 有没有类似strtok的函数
for string, you can use basic_string<>::find_first_of()
M**u
发帖数: 10158
46
来自主题: Programming版 - 有没有类似strtok的函数
how about stringstream...
t****t
发帖数: 6806
47
来自主题: Programming版 - 有没有类似strtok的函数
i said you can generate istream_iterator and use find_first_of() in <
algorithm>
m*******l
发帖数: 12782
d****i
发帖数: 4809
49
来自主题: Programming版 - 有码工的地方就有是非啊
这个他们吵得什么?C的strtok()的实现?
d****i
发帖数: 4809
50
来自主题: Programming版 - 有码工的地方就有是非啊
这个他们吵得什么?C的strtok()的实现?
1 (共1页)