如何打印递增的“计数”?从这个函数变量? [英] How can I print the incremented "count" variable from this function?

查看:105
本文介绍了如何打印递增的“计数”?从这个函数变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  void  parse( char  * str)
{
int left = 0 ,right = 0 ,count = 0 ;
int len = strlen(str);
printf( %d,count);

while (右< = len&& left< = right){
if (isDelimiter(str [right])== false
right ++;

if (isDelimiter(str [right])== true & & left == right){
if (isOperator(str [right])== true ){
printf( '%c'是操作员'\\ n ,str [right]);
count = count + 1;
}

right ++;
left = right;
} else if (isDelimiter(str [right])== true && left!= right
||(right == len&& left!= right)){
char * subStr = subString(str,left,right - 1 );

if (isKeyword(subStr)== true ){
printf( '%s'是关键字\ n,subStr);
count = count + 1;
}

else if (isInteger(subStr)== true ){
printf( '% s'是一个INTEGER \ n,subStr);
count = count + 1;
}

else if (isRealNumber(subStr)== true ){
printf( '% s'是真实的数字\ n,subStr);
count = count + 1;
}

else if (validIdentifier(subStr)== true
&& isDelimiter(str [right - 1 ])== false ){
printf( '%s'是一个有效的标识符\ n,subStr);
count = count + 1;
}

else if (validIdentifier(subStr)== false
&& isDelimiter(str [right - 1 ])== false ){
printf( '%s'不是有效的标识符\ n,subStr);
count = count + 1;
}
left = right;
}
}
return ;
}

// DRIVER FUNCTION
< span class =code-keyword> int main()
{
// 此处字符串的最大长度为100
char str [ 100 ] = int a = b + 1c;;

parse(str); // 调用解析函数

return 0 );
}





我的尝试:



i尝试返回一个整数值,但该函数也有字符。

i需要递增的计数值。

我需要打印计数 每次执行循环时递增的变量

解决方案

有点不清楚你的意思,但是如果你想将字符添加到长度而不是添加每个字符为1或添加字符串长度。



您正确使用了strlen函数:

  char  str [ 100 ] =  < span class =code-string> int a = b + 1c;; 
int len = strlen(str);
// 或添加到其他长度
totalLength + = len;
printf( %d,len);


void parse(char* str)
{
    int left = 0, right = 0, count=0;
    int len = strlen(str);
	printf("%d",count);
 
    while (right <= len && left <= right) {
        if (isDelimiter(str[right]) == false)
            right++;
 
        if (isDelimiter(str[right]) == true && left == right) {
            if (isOperator(str[right]) == true){
                printf("'%c' IS AN OPERATOR\n", str[right]);
				count=count+1;
			}
 
            right++;
            left = right;
        } else if (isDelimiter(str[right]) == true && left != right
                   || (right == len && left != right)) {
            char* subStr = subString(str, left, right - 1);
 
            if (isKeyword(subStr) == true){
                printf("'%s' IS A KEYWORD\n", subStr);
				count=count+1;
			}
 
            else if (isInteger(subStr) == true){
                printf("'%s' IS AN INTEGER\n", subStr);
				count=count+1;
			}
 
            else if (isRealNumber(subStr) == true){
                printf("'%s' IS A REAL NUMBER\n", subStr);
				count=count+1;
			}
 
            else if (validIdentifier(subStr) == true
                     && isDelimiter(str[right - 1]) == false){
                printf("'%s' IS A VALID IDENTIFIER\n", subStr);
				count=count+1;
			}
 
            else if (validIdentifier(subStr) == false
                     && isDelimiter(str[right - 1]) == false){
                printf("'%s' IS NOT A VALID IDENTIFIER\n", subStr);
				count=count+1;
			}
            left = right;
        }
    }
    return ;
}
 
// DRIVER FUNCTION
int main()
{
     // maximum legth of string is 100 here 
    char str[100] = "int a = b + 1c; ";
 
    parse(str); // calling the parse function
 
    return (0);
}



What I have tried:

i tried returning an integer value, but the function also has character.
i need the incremented value of the count.
I need to print the "count" variable that is incremented each time the loop is executed

解决方案

It is a bit unclear what you mean, but if you want to add the characters to the lenght than add 1 for every char or add the the string length.

You correctly used the strlen function:

char str[100] = "int a = b + 1c; ";
int len = strlen(str);
//or add to some other length
totalLength += len;
printf("%d", len);


这篇关于如何打印递增的“计数”?从这个函数变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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