C-计算字符串中的单词
我一直在尝试在C中使用一个函数来计算字符串中的字数。但是,在某些casa(如示例中的casa)中,它应该返回0而不是1。。。有什么想法可能是错误的吗C-计算字符串中的单词,c,string,C,String,我一直在尝试在C中使用一个函数来计算字符串中的字数。但是,在某些casa(如示例中的casa)中,它应该返回0而不是1。。。有什么想法可能是错误的吗 #import <stdio.h> int contaPal(char s[]) { int r; int i; r = 0; for (i = 0; s[i] != '\0'; i++) { if (s[i]
#import <stdio.h>
int contaPal(char s[]) {
int r;
int i;
r = 0;
for (i = 0; s[i] != '\0'; i++) {
if (s[i] == '\n')
r = r + 0;
if (s[i] != ' ' && s[i + 1] == ' ' && s[i + 1] != '\0')
r++;
if (s[i] != ' ' && s[i + 1] == '\0') {
r++;
}
}
return r;
}
int main () {
char s[15] = { ' ', '\n', '\0' };
printf("Words: %d \n", (contaPal(s)));
return 0;
}
#导入
int contaPal(字符s[]){
INTR;
int i;
r=0;
对于(i=0;s[i]!='\0';i++){
如果(s[i]='\n')
r=r+0;
如果(s[i]!=''&&s[i+1]=''&&s[i+1]!='\0')
r++;
如果(s[i]!=''&&s[i+1]='\0'){
r++;
}
}
返回r;
}
int main(){
字符s[15]={'','\n','\0'};
printf(“单词:%d\n”,(续);
返回0;
}
行:
if (s[i] != ' ' && s[i + 1] == ' ' && s[i + 1] != '\0')
r++;
当您查看'\n'
时,与大小写完全匹配
如果。。。否则,如果…行:
if (s[i] != ' ' && s[i + 1] == ' ' && s[i + 1] != '\0')
r++;
当您查看'\n'
时,与大小写完全匹配
如果。。。否则,如果…您不应将
'\n'
与任何其他空白字符区别对待
以下是一个更简单的版本:
#include <ctype.h>
#include <stdio.h>
int contaPal(const char *s) {
int count = 0, hassep = 1;
while (*s) {
if (isspace((unsigned char)*s) {
hassep = 1;
} else {
count += hassep;
hassep = 0;
}
s++;
}
return count;
}
int main(void) {
char s[] = " \n";
printf("Words: %d\n", contaPal(s));
return 0;
}
#包括
#包括
int contaPal(const char*s){
int count=0,hassep=1;
而(*s){
if(isspace((无符号字符)*s){
hassep=1;
}否则{
计数+=hassep;
hassep=0;
}
s++;
}
返回计数;
}
int main(void){
字符s[]=“\n”;
printf(“单词:%d\n”,续);
返回0;
}
您不应将'\n'
与任何其他空白字符区别对待
以下是一个更简单的版本:
#include <ctype.h>
#include <stdio.h>
int contaPal(const char *s) {
int count = 0, hassep = 1;
while (*s) {
if (isspace((unsigned char)*s) {
hassep = 1;
} else {
count += hassep;
hassep = 0;
}
s++;
}
return count;
}
int main(void) {
char s[] = " \n";
printf("Words: %d\n", contaPal(s));
return 0;
}
#包括
#包括
int contaPal(const char*s){
int count=0,hassep=1;
而(*s){
if(isspace((无符号字符)*s){
hassep=1;
}否则{
计数+=hassep;
hassep=0;
}
s++;
}
返回计数;
}
int main(void){
字符s[]=“\n”;
printf(“单词:%d\n”,续);
返回0;
}
我想这个词是不包括空格字符的任何字符序列
函数返回1,因为在遇到新行字符时,对于提供的字符串,变量r
会由于这种情况而增加
if (s[i] != ' ' && s[i + 1] == '\0') {
r++;
}
所以函数实现是错误的
可以按照演示程序中所示的方式对其进行定义
#include <stdio.h>
#include <ctype.h>
size_t contaPal( const char s[] )
{
size_t n = 0;
while ( *s )
{
while ( isspace( ( unsigned char )*s ) ) ++s;
n += *s != '\0';
while ( *s && !isspace( ( unsigned char )*s ) ) ++s;
}
return n;
}
int main(void)
{
char s[] = { ' ', '\n', '\0' };
printf( "Words: %zu\n", contaPal( s ) );
return 0;
}
我假设这个词是不包括空格字符的任何字符序列 函数返回1,因为在遇到新行字符时,对于提供的字符串,变量
r
会由于这种情况而增加
if (s[i] != ' ' && s[i + 1] == '\0') {
r++;
}
所以函数实现是错误的
可以按照演示程序中所示的方式对其进行定义
#include <stdio.h>
#include <ctype.h>
size_t contaPal( const char s[] )
{
size_t n = 0;
while ( *s )
{
while ( isspace( ( unsigned char )*s ) ) ++s;
n += *s != '\0';
while ( *s && !isspace( ( unsigned char )*s ) ) ++s;
}
return n;
}
int main(void)
{
char s[] = { ' ', '\n', '\0' };
printf( "Words: %zu\n", contaPal( s ) );
return 0;
}
使用现有字符测试功能的简单说明:
int main(void)
{
int cnt = 0;
int numWords = 0;
BOOL trap = 0; //start count only after seeing a word
char *sentence = "This is a sentence, too long.";
//char *sentence2 = " ";//tested for empty string also
while (*sentence != '\0')
{
if ( isalnum (*sentence) ) //word is found, set trap and start count
{
sentence++; //alpha numeric character, keep going
trap = 1;
}
else if ( (( ispunct (*sentence) ) || ( isspace(*sentence) )) && trap)
{ //count is started only after first non delimiter character is found
numWords++;
sentence++;
while(( ispunct (*sentence) ) || ( isspace(*sentence) ))
{ //handle sequences of word delimiters
sentence++;
}
}
else //make sure pointer is increased either way
{
sentence++;
}
}
return 0;
}
使用现有字符测试功能的简单说明:
int main(void)
{
int cnt = 0;
int numWords = 0;
BOOL trap = 0; //start count only after seeing a word
char *sentence = "This is a sentence, too long.";
//char *sentence2 = " ";//tested for empty string also
while (*sentence != '\0')
{
if ( isalnum (*sentence) ) //word is found, set trap and start count
{
sentence++; //alpha numeric character, keep going
trap = 1;
}
else if ( (( ispunct (*sentence) ) || ( isspace(*sentence) )) && trap)
{ //count is started only after first non delimiter character is found
numWords++;
sentence++;
while(( ispunct (*sentence) ) || ( isspace(*sentence) ))
{ //handle sequences of word delimiters
sentence++;
}
}
else //make sure pointer is increased either way
{
sentence++;
}
}
return 0;
}
@João Pimentel定义单词的概念。@João Pimentel如果遇到新行,则r会由于这种情况而增加,如果(s[i]!=''&&s[i+1]='\0'){。因此,函数将为您定义的字符串返回至少1。at
如果(s[i]!=''&&s[i+1]='\0'){
:('\n'!=''&&&&'\0'='0')
变为真。不要逐个检查空白字符,它们有很多,这会使您的任务复杂化(''''\t','\n','\r'
),请使用ctype.h
中的isspace
。r=r+0?呵呵:)@João Pimentel定义单词的概念。@João Pimentel如果遇到新行,则r会由于这种情况而增加,如果(s[i]!=''&&s[i+1]='\0'){。因此,函数将为您定义的字符串返回至少1。at如果(s[i]!=''&&s[i+1]='\0'){
:('\n'!=''&&&&'\0'='0')
变为真。不要逐个检查空白字符,它们有很多,这会使您的任务复杂化(''''\t','\n','\r'
),请使用ctype.h
中的isspace
。r=r+0?呵呵:)@VladfromMoscow-我没有实现一个函数,它是一个代码片段,说明了如何使用附加的char测试函数ispunt
和isspace
。请问您指的是哪个函数。顺便说一句,我在一个小的主函数中测试了这一点。在任何情况下,代码片段都是函数的一部分,不是吗?这是错误的。一个只包含空格的句子会有什么结果?@VladfromMoscow-仍然不确定你指的是什么,但重新编写以包含main()
,围绕着代码片段的其余部分。这是否解决了您的问题?@A.S.H-啊!谢谢。一个错误的计数。我需要在开始计数之前至少测试一个非分隔符。@VladFrommosco-我没有实现函数,它是一个代码片段,说明了如何使用其他字符测试函数ispunt
和isspace
。请问您指的是哪个函数。顺便说一句,我在一个小的主函数中测试过。无论如何,代码片段是函数的一部分,不是吗?这是错误的。如果一个句子只包含空格,结果会是什么?@vladfrommosco-仍然不确定您指的是什么,但重新编写以包含main()。