所以我是C的新手,正在自学字符串处理。到目前为止,我的问题是我的功能是什么标记sp?输入的数字串不仅仅是在空格上切割。例如:如果我输入一个像45这样的数字,我的数组中的结果字符串将同时显示45和5,因此在两位数字中,无论出于何种原因,它都会分割数字。我已经仔细搜索过但没有运气。
希望我只是忽略了一个明显的错误。但是我已经达到了无法继续学习的程度,所以对任何帮助都表示赞赏!
示例输出:
please enter your string: 1 45 30 82
converting strings to ints
Printing the string
1, 0, 45, 5, 0, 30, 0, 0, 82, 2,
Press any key to continue . . .
我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#define STRING_LENGTH 81
#define MAX_TOKENS 40
int StrInput( char dataStr[]);
void atoiWorker( char dataStr[], char results[], int idx);
void printer ( char dataStr[], int idx);
void tokenize ( char dataStr[], char results[]);
int main()
{
int idx;
char dataStr[STRING_LENGTH];
char results[STRING_LENGTH];
idx = StrInput(dataStr);
tokenize(dataStr, results);
atoiWorker(dataStr, results, idx);
printer(results, idx);
}
int StrInput(char dataStr[])
{
int idx = 0;
printf( "please enter your string: " );
while (idx < (STRING_LENGTH) && ((dataStr[idx] = getchar()) != '\n'))
idx++;
dataStr[idx] = '\0';
return idx;
}
void atoiWorker( char dataStr[], char results[], int idx)
{
int i;
printf( "converting strings to ints\n" );
for (i = 0; i < idx; i++)
results[i] = atoi(&dataStr[i]);
}
void tokenize(char dataStr[], char *results[])
{
int count = 0;
char delim[] = " ,\t\n"; //found this on msdn, hopefully it's right
if (results[0] = strtok(dataStr, " \t"))
count++;
while (results[count] = strtok(NULL, delim/*" \t"*/))
count++;
}
void printer(char dataStr[], int idx)
{
int i;
printf( "Printing the string\n" );
for (i = 0; i < idx; i++)
printf( " %d,", dataStr[i] );
printf( "\n" );
}
答案 0 :(得分:1)
在results[]
中设置tokenize()
之后,您将在atioWorker()
中覆盖它。
答案 1 :(得分:1)
像这样修复
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#define STRING_LENGTH 81
#define MAX_TOKENS 40
void StrInput( char dataStr[]);
void atoiWorker( char *tokens[], int results[], int idx);
void printer ( int results[], int idx);
int tokenize ( char dataStr[], char *results[]);
int main()
{
int idx;
char dataStr[STRING_LENGTH];
char *tokens[MAX_TOKENS];
int results[MAX_TOKENS];
StrInput(dataStr);
idx = tokenize(dataStr, tokens);
atoiWorker(tokens, results, idx);
printer(results, idx);
}
void StrInput(char dataStr[])
{
int idx = 0;
printf( "please enter your string: " );
while (idx < (STRING_LENGTH) && ((dataStr[idx] = getchar()) != '\n'))
idx++;
dataStr[idx] = '\0';
}
void atoiWorker( char *tokens[], int results[], int idx)
{
int i;
printf( "converting strings to ints\n" );
for (i = 0; i < idx; i++)
results[i] = atoi(tokens[i]);
}
int tokenize(char dataStr[], char *results[])
{
int count = 0;
char delim[] = " ,\t\n"; //found this on msdn, hopefully it's right
if (results[0] = strtok(dataStr, " \t"))
count++;
while (results[count] = strtok(NULL, delim/*" \t"*/))
count++;
return count;
}
void printer(int result[], int idx)
{
int i;
printf( "Printing the string\n" );
for (i = 0; i < idx; i++)
printf( " %d,", result[i] );
printf( "\n" );
}