Untitled

 avatar
unknown
plain_text
3 years ago
784 B
12
Indexable
void initialization_Q2()
{
    int number = 0;
    char str_1[20];
    cout<<"Enter your number -> :"<<endl;
    cin>>number;
    cout << "Enter your string -> :"<<endl;
    cin>>str_1;
    int x = words(str_1, number);
    cout<<x<<endl;
}

int words(char str[], int length)
{
   // char helper_string[20];
    char temporary_string[20];
    int flag = 0;
    int i = 0;
    int counter = 0;
    
    while (str[i] != '\0')
    {
        i++;
        
        if (str[i] == ' ')
        {
            flag = 1;
            strcpy(temporary_string, str);
            if (strlen(temporary_string) == length)
            {
                counter ++;
                cout<<temporary_string;
                temporary_string[0] = NULL;
            }
        }
    }
    return counter;
}
Editor is loading...