在 C 中计算空格 -argc/argv
Counting whitespaces -argc/argv in C
我想计算空格,例如 ' '
(ASCII SP 或 32)。我必须使用命令行传递参数。因此,例如,我输入 Hello World 并希望接收空格的数量,在这种情况下结果应该是 2。
我已经尝试过以下操作:
文本 = 你好世界
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]){
int spaces = 0;
char* mystring = argv[1];
int size = strlen(argv[1]) + strlen(argv[2]);
for(int i = 0; i < size; i++){
if((*(mystring + i)) == ' '){
spaces++;
printf("%d\n", spaces);
}
}
}
我知道 *(argv + 1)
是 Hello
(或 ASCII 码)并且 *(argv + 2)
= World
这就是我遇到的问题。如何计算 argv[n]
之间的空格?空格的数量可能不同,因此我不想像 If(argc > 1){ spaces++;}
.
这样的代码
有人可以帮忙吗?
此致,
凯塔
像这样用双引号传递字符串 "Hello World"。
如果你执行:
$ a.out Hello world # There are 5 spaces between both args here.
shell 将通过在序列 os space 的 pos 处将输入命令拆分为参数来提取命令的参数(连续的 space 序列,制表符 and/or 换行符)和注释(如上面的注释)从输入中删除,因此如果您发出上面的命令,您将得到一个 argv
像这样:
int argc = 3;
char *argv[] = { "a.out", "Hello", "world", NULL, };
如果您使用引号分隔参数,您可以发布
$ a.out "Hello world" # there are also 5 spaces between the words.
在这种情况下你会得到类似的东西:
int argc = 2;
char *argv[] = { "a.out", "Hello world", NULL, };
在这种情况下,您会将 space 放入参数中。
重要
您不检查传递给 a.out
的参数数量,因此在您 post 的情况下,您可以尝试将 NULL
指针传递给 strlen()
这将导致未定义的行为。这是一个错误,为了让您的程序正常工作,您可以执行以下操作(我已经更正了一些其他错误并在代码的注释中对它们进行了注释):
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
int spaces = 0;
int arg_ix; /* index of the argument to consider */
for (arg_ix = 1; arg_ix < argc; arg_ix++) { /* so we don't check more arguments than available */
char *mystring = argv[arg_ix];
int size = strlen(mystring);
for(int i = 0; i < size; i++) {
if(mystring[i] == ' '){ /* why use such hell notation? */
spaces++;
}
}
}
printf("%d\n", spaces); /* print the value collected at the end, not before */
}
并且可以使用这种方法简化此代码(利用 mystring
作为指针,通过移动指针直到我们到达字符串的末尾(指向 [=22=]
字符)(它还避免了计算字符串长度,这使得对字符串的另一次传递---不必要)
#include <stdio.h>
/* string.h is not needed anymore, as we don't use strlen() */
int main(int argc, char* argv[]){
int spaces = 0;
int arg_ix;
for (arg_ix = 1; arg_ix < argc; arg_ix++) {
char* mystring = argv[arg_ix];
for( /* empty */; *mystring != '[=15=]'; mystring++) {
if(*mystring == ' '){
spaces++;
}
}
}
printf("%d\n", spaces);
}
最后,你有一个 <ctype.h>
header 和 isspace(c)
之类的函数来检查一个字符是否是 space (在这种情况下,它检查 space 和制表符。
#include <stdio.h>
#include <ctype.h>
int main(int argc, char* argv[]){
int spaces = 0;
int arg_ix;
for (arg_ix = 1; arg_ix < argc; arg_ix++) {
char* mystring = argv[arg_ix];
for(; *mystring != '[=16=]'; mystring++) {
if(isspace(*mystring)){
spaces++;
}
}
}
printf("%d\n", spaces);
}
我想计算空格,例如 ' '
(ASCII SP 或 32)。我必须使用命令行传递参数。因此,例如,我输入 Hello World 并希望接收空格的数量,在这种情况下结果应该是 2。
我已经尝试过以下操作: 文本 = 你好世界
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[]){
int spaces = 0;
char* mystring = argv[1];
int size = strlen(argv[1]) + strlen(argv[2]);
for(int i = 0; i < size; i++){
if((*(mystring + i)) == ' '){
spaces++;
printf("%d\n", spaces);
}
}
}
我知道 *(argv + 1)
是 Hello
(或 ASCII 码)并且 *(argv + 2)
= World
这就是我遇到的问题。如何计算 argv[n]
之间的空格?空格的数量可能不同,因此我不想像 If(argc > 1){ spaces++;}
.
有人可以帮忙吗?
此致,
凯塔
像这样用双引号传递字符串 "Hello World"。
如果你执行:
$ a.out Hello world # There are 5 spaces between both args here.
shell 将通过在序列 os space 的 pos 处将输入命令拆分为参数来提取命令的参数(连续的 space 序列,制表符 and/or 换行符)和注释(如上面的注释)从输入中删除,因此如果您发出上面的命令,您将得到一个 argv
像这样:
int argc = 3;
char *argv[] = { "a.out", "Hello", "world", NULL, };
如果您使用引号分隔参数,您可以发布
$ a.out "Hello world" # there are also 5 spaces between the words.
在这种情况下你会得到类似的东西:
int argc = 2;
char *argv[] = { "a.out", "Hello world", NULL, };
在这种情况下,您会将 space 放入参数中。
重要
您不检查传递给 a.out
的参数数量,因此在您 post 的情况下,您可以尝试将 NULL
指针传递给 strlen()
这将导致未定义的行为。这是一个错误,为了让您的程序正常工作,您可以执行以下操作(我已经更正了一些其他错误并在代码的注释中对它们进行了注释):
#include <stdio.h>
#include <string.h>
int main(int argc, char* argv[])
{
int spaces = 0;
int arg_ix; /* index of the argument to consider */
for (arg_ix = 1; arg_ix < argc; arg_ix++) { /* so we don't check more arguments than available */
char *mystring = argv[arg_ix];
int size = strlen(mystring);
for(int i = 0; i < size; i++) {
if(mystring[i] == ' '){ /* why use such hell notation? */
spaces++;
}
}
}
printf("%d\n", spaces); /* print the value collected at the end, not before */
}
并且可以使用这种方法简化此代码(利用 mystring
作为指针,通过移动指针直到我们到达字符串的末尾(指向 [=22=]
字符)(它还避免了计算字符串长度,这使得对字符串的另一次传递---不必要)
#include <stdio.h>
/* string.h is not needed anymore, as we don't use strlen() */
int main(int argc, char* argv[]){
int spaces = 0;
int arg_ix;
for (arg_ix = 1; arg_ix < argc; arg_ix++) {
char* mystring = argv[arg_ix];
for( /* empty */; *mystring != '[=15=]'; mystring++) {
if(*mystring == ' '){
spaces++;
}
}
}
printf("%d\n", spaces);
}
最后,你有一个 <ctype.h>
header 和 isspace(c)
之类的函数来检查一个字符是否是 space (在这种情况下,它检查 space 和制表符。
#include <stdio.h>
#include <ctype.h>
int main(int argc, char* argv[]){
int spaces = 0;
int arg_ix;
for (arg_ix = 1; arg_ix < argc; arg_ix++) {
char* mystring = argv[arg_ix];
for(; *mystring != '[=16=]'; mystring++) {
if(isspace(*mystring)){
spaces++;
}
}
}
printf("%d\n", spaces);
}