可能有人给我解释一下什么样的差异之间存在着strtok()
和strsep()
?它们的优点和缺点是什么?为什么我会选择一个而不是另一个.
在C中如何用分隔符分隔char数组?或者操纵字符串更好?有什么好的C char操作函数?
在C中,如果我有:
char *reg = "[R5]";
Run Code Online (Sandbox Code Playgroud)
而且我要
char *reg_alt = "R5"
(相同的东西,但没有括号),我该怎么做?
我试过了
*char reg_alt = reg[1:2];
Run Code Online (Sandbox Code Playgroud)
但这不起作用.
您好,我需要有关具有某些 PID 的进程的以下信息:
名称、ppid、状态、#ofOpenFiles、#ofThreads
我知道 /proc/pid/stat 文件的示例如下:
15(看门狗/1) S 2 0 0 0 -1 69239104 0 0 0 0 0 69 0 0 -100 0 1 0 6 0 0 18446744073709551615 0 0 0 0 0 0 0 2147483647 0 1844674407370 9551615 0 0 17 1 99 1 0 0 0 0 0 0 0 0 0 0 0
我当前尝试解析此类文件:
FILE *fp;
char buff[255];
fp= fopen("/proc/123/stat", "r");
if(fp == NULL){
}else{
fscanf(fp, "%d %s %c %d %d %d %d %d …
Run Code Online (Sandbox Code Playgroud) 我创建了一个.ini
名为configuration.ini
. 该文件如下所示:
[Application]
no_of_applications= 4;
[states]
title = "Config File States available";
name = 'Active, Deactivated, Hot';
[reaction]
reac_type = ["switch_schedule", "activate_app", "disable_app";
Run Code Online (Sandbox Code Playgroud)
现在我打算从我的 C 程序中读取这个 .ini 文件。我已经#include <configuration.ini>
在我的 C 程序中包含了这一行。我只想阅读 C 程序中“状态”部分的标题,即
#include<stdio.h>
#include<configuration.ini>
int main()
{
//read the variable 'title' from 'states' and print it
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有人可以告诉我我该怎么做?
我正在编写一个C程序来研究strtok()用法的用法.这是我的代码:
#include <stdio.h>
#include <string.h>
main() {
char abc[100] = "ls &";
char *tok;
tok = strtok(abc, " ");
while (tok != NULL) {
printf("%s", tok);
tok = strtok(NULL, " ");
}
printf("\n\n\n\n\n%s", tok);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它打印以下输出:
ls&
(null)
Run Code Online (Sandbox Code Playgroud)
但我希望它在第二个printf语句中打印'&'.我该怎么做?我需要这部分作为我的作业项目.谁能帮我吗?
先感谢您!:)
我正在尝试编写一个代码来删除句子空格的所有前导、尾随和中间,但在单词之间只保留一个空格。
例如,如果输入是
" This is my string "
Run Code Online (Sandbox Code Playgroud)
输出应该是
"This is my string"
Run Code Online (Sandbox Code Playgroud)
到目前为止,我想出了这个:
#include <stdio.h>
void cascade(char str[], int i);
main()
{
char str[100] = " This is my string ";
int i = 0;
while (str[i] != '\0') {
if (str[i] == ' ' && str[i + 1] == ' ')
cascade(str, i);
i++;
}
printf("%s\n", str);
}
void cascade(char str[], int i)
{
while(str[i] != '\0') {
str[i] = str[i + 1];
i++;
}
str[i + 1] …
Run Code Online (Sandbox Code Playgroud) 我有一个包含两个名字和一个逗号的字符串我怎么能把它们分开并将它们写成单独的字符串.
例
char *line="John Smith,Jane Smith";
Run Code Online (Sandbox Code Playgroud)
我正在考虑使用sscanf函数.
sscanf(line,"%s,%s",str1,str2);
Run Code Online (Sandbox Code Playgroud)
我该怎么办?
注意:我可以将逗号更改为空格字符.
例如,我有一个字符串" /home/george/file.c
",我想将字符串剪切为" file.c
",所以只需删除最后一个字符前的所有字符串/
.
我写了一个程序来查找字符串中最长的单词并打印最长单词中的字母数.但代码不是打印.我多次分析了该程序,但我找不到解决方案.
#include <stdio.h>
#include <string.h>
int main() {
char string[100] = "Hello Kurnool";
int i = 0, letters = 0, longest = 0;
start:
for (; string[i] !=' '; i++) {
letters++;
}
if (letters >= longest)
longest = letters;
if (string[i] == ' ') {
letters = 0;
i++;
goto start;
}
printf("%d", longest);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试从 IP 地址获取 4 个整数。例如,12.34.56.78。A = 12,b = 34,c = 56,d = 78。这是我的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ADDRESS[100];
printf("Enter IP: ");
scanf("%s", ADDRESS);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我怎么能做到这一点?