用C字计数 [英] Counting characters in C

查看:131
本文介绍了用C字计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写计数字符串中的所有字符的程序。我本来它,但后来意识到我不能指望空间。我不明白为什么这是行不通的。

 为(M = 0; Z [M] = 0;!M +){
    如果(Z [M]!=''){
        charcount ++;
    }
}

任何帮助,AP preciated。

编辑*是否有所作为,如果输入(弦)正在扫描的这样吗?是的,一切都被初始化。我试着打印什么Z [M]计算过,这是不是在M字符串的实际值,我想这就是问题所在。

 为(J = 0; J< 7; J ++){
    的printf(请输入一个字符串:\\ n);
    scanf函数(%S,Z);
        为(M = 0; Z [M] = 0;!M +){
                如果(Z [M]!=''){
                charcount ++;
                }
        }


解决方案

您需要初始化 charcount 。除此之外,它应该工作,但前提是以Z 是字符的零结尾的数组和 M INT 或相似。我可能会写的只是 Z [M] ,而不是 Z [M]!= 0 (因为!0 =真0 =假),但两者的工作。有这样做的更有效的方法(虽然这几天我敢打赌,编译器将处理转换到这一点给你一个基于指针的循环)。

下面是用最少的编辑一个完整的,正确的示例:

 为const char * Z =测试一二三
INT米;
INT charcount;charcount = 0;
为(M = 0; Z [M]; M +){
    如果(Z [M]!=''){
        charcount ++;
    }
}

如果您使用的是字符串类某种而非老式的C语言的零终止的数组,你要看看那类如何通过它循环。

以上所有还假定你正在处理的ASCII字符串。如果你正在处理UTF-EN codeD字符串,你必须处理多字节字符。


重新您的编辑:它使一个很大的区别: scanf函数会的停在第一个空白(我忘了)。它可能是比一个更大的区别,不过,如果你不声明以Z 正确。 (我也建议你使用时, scanf函数读字符串[或避免 scanf函数完全]使用字段宽度;否则, ,您对字符的数量无法控制它会尝试存储等的在理论上的,没有缓冲都不会大到足以避免溢出这里更多:的 http://www.crasseux.com/books/ctutorial/String-overflows-with-scanf。 HTML

I'm trying to write a program that counts all the characters in a string. I originally had it, but then realized I can't count spaces. I can't see why this does not work.

for(m=0; z[m] != 0; m++) {
    if(z[m] != ' ') {
        charcount ++;
    }
}

Any assistance appreciated.

Edit* Does it make a difference if the input(strings) are being scanned in like this? And yes, everything is initialized. I've tried printing what z[m] evaluates too and it isn't the actual value of the string at "m", I think this is the problem.

for(j=0; j<7; j++){
    printf("Enter a string:\n");
    scanf("%s", z);
        for(m=0; z[m] != 0; m++){
                if(z[m] != ' '){
                charcount ++;
                }
        }

解决方案

You need to initialize charcount. Other than that, it should work, provided that z is a zero-terminated array of characters and m is an int or similar. I would probably write just z[m] rather than z[m] != 0 (since !0 = true and 0 = false), but both work. There are more efficient ways of doing it (although these days I bet a compiler will handle converting this into a pointer-based loop for you).

Here's a complete, correct example with minimal edits:

const char * z = "testing one two three";
int m;
int charcount;

charcount = 0;
for(m=0; z[m]; m++) {
    if(z[m] != ' ') {
        charcount ++;
    }
}

If you're using a String class of some kind rather than an old-fashioned C null-terminated array, you'll want to look at that class for how to loop through it.

All of the above also assumes you're dealing with ASCII strings. If you're dealing with UTF-encoded strings, you have to handle multi-byte characters.


Re your edit: It makes a big difference: scanf will stop on the first blank (I'd forgotten that). It might make an even bigger difference than that, though, if you're not declaring z correctly. (I'd also recommend using a field width when using scanf for reading strings [or avoiding scanf entirely]; otherwise, you have no control over the number of chars it will try to store, and so in theory, no buffer will ever be big enough to avoid an overflow. More here: http://www.crasseux.com/books/ctutorial/String-overflows-with-scanf.html)

这篇关于用C字计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆