arrayscstringloopsstrstr

How many this word in the array string


I want to count how many this word and operator in the string but I try to use strchr and it doesn't work.

#include <stdio.h>
#include <string.h>
#include <ctype.h>


int main()
{
    int x,count =0;
    char buff[100]="1+2.3(7^8)sin cos + cos sin_e-2x+x2*2!/_x1 sine";
    //gets(buff);
    strupr(buff);
    for (int i = 0; buff[i] != '\0'; i++)
    {
        if (buff[i] == '+' || buff[i] == '-' || buff[i] == '*' ||
                buff[i] == '/' || buff[i] == '^'|| buff[i] == '(')
        {
            count++;
        }

    }
    char *op2;
    int check=0;
    char cpysin[100],cpycos[100];
    strcpy(cpysin,buff);
    strcpy(cpycos,buff);


    do
    {
        if(strchr(cpysin,'SIN')!=0)
        {
            count++;
            strcpy(cpysin,strstr(cpysin,"SIN"));
            cpysin[0] = ' ';
            cpysin[1] = ' ';
            cpysin[2] = ' ';
        }
        else
        {
            break;
        }
    }
    
    while(check==0);
    do
    {
        if(strchr(cpycos,'COS')!=0)
        {
            count++;
            strcpy(cpycos,strstr(cpycos,"COS"));
            cpycos[0] = ' ';
            cpycos[1] = ' ';
            cpycos[2] = ' ';
        }
        else
        {
            break;
        }
    }
    while(check==0);
    printf("FINAL \n%d",count);
}

I only work when I do it in the loop while trying to find how many sins are in there but it doesn't work when I put cos function on it. Please tell me how to fix this and what if I need to write more functions to find.


Solution

  • strchr(cpysin, 'SIN') is wrong.

    Unfortunately the compiler may not give you a warning because 'SIN' can be interpreted as 4 byte integer. The second parameter is supposed to be an integer, but strchr really wants character, it chops it off to 'N'

    Just remember that in C you work with single characters 'a' or strings "cos" (or you can come accross wide characters/strings)

    Use strstr to find a string. For example, to find "cos":

    char* ptr = buff;
    char* find = strstr(ptr, "cos");
    
    "1+2.3(7^8)sin cos + cos sin_e-2x+x2*2!/_x1 sine";
    ---------------^ <- find
    

    find will point to "cos + cos sin_e-2x+x2*2!/_x1 sine"

    You can increment ptr and look for the next occurrence of "cos".

    Also note, you can declare char buff[] = "...", you don't have to assign the buffer size.

    char buff[] = "1+2.3(7^8)sin cos + cos sin_e-2x+x2*2!/_x1 sine";
    int count = 0;
    const char* ptr = buff;
    const char* text = "cos";
    
    //must terminate when ptr reaches '\0' which is at the end of buff
    //there is serious problem if we read past it
    while(*ptr != '\0')
    {
        char* find = strstr(ptr, text);
        if (find != NULL)
        {
            printf("update [%s]\n", find);
            count++;
            ptr = find + strlen(text);
            //next search starts after
        }
        else
        {
            ptr++;
            //next character start at next character
        }
    }
    printf("%s count: %d\n", text, count);