9.3.21

Spring 2021 C++ : hw-2

1. Write a OOP to read string using cin function in c ++ . Count the number of character in that string without using built-in function.

2. Write a OOP to read a line of string using getline() function in c++. Count the number of word and number of character from that string. 

5 comments:

  1. #include
    using namespace std;
    class B
    {
    public:
    char str[10];
    int i,total=0;
    void inp()
    {
    cout<<"Enter an any string: ";
    cin>>str;


    for( i=0;str[i]!='\0';i++)
    {
    total++;

    }
    }

    };
    int main()
    {
    B obj;
    obj.inp();
    cout<<"The total number of string is: "<
    using namespace std;
    class B
    {
    public:
    char a[200];
    int counts()
    {
    cout<< "Enter a string: ";
    cin.getline(a,200);
    int x, words = 0;

    for(int i = 0; a[i] != '\0'; i++)
    {
    if (a[i] == ' ')
    {
    words++;
    }
    }
    cout << "The number of string = " << words+1<< endl;
    return 0;
    }
    };
    class B1: public B
    {
    public:
    char str[10];
    int i,total=0;
    void inp()
    {
    cout<<"Enter an any character: ";
    cin>>str;


    for( i=0;str[i]!='\0';i++)
    {
    total++;

    }
    }

    };
    int main()
    {
    B1 obj;
    obj.counts();
    obj.inp();
    cout<<"The total number of string is: "<<obj.total<<endl;

    }

    ReplyDelete
  2. #include
    using namespace std;
    class B
    {
    public:
    char str[10];
    int i,total=0;
    void inp()
    {
    cout<<"Enter an any string: ";
    cin>>str;


    for( i=0;str[i]!='\0';i++)
    {
    total++;

    }
    }

    };
    int main()
    {
    B obj;
    obj.inp();
    cout<<"The total number of string is: "<<obj.total<<endl;



    }

    ReplyDelete
  3. #include
    using namespace std;
    class B
    {
    public:
    char a[200];
    int counts()
    {
    cout<< "Enter a string: ";
    cin.getline(a,200);
    int x, words = 0;

    for(int i = 0; a[i] != '\0'; i++)
    {
    if (a[i] == ' ')
    {
    words++;
    }
    }
    cout << "The number of string = " << words+1<< endl;
    return 0;
    }
    };
    class B1: public B
    {
    public:
    char str[10];
    int i,total=0;
    void inp()
    {
    cout<<"Enter an any character: ";
    cin>>str;


    for( i=0;str[i]!='\0';i++)
    {
    total++;

    }
    }

    };
    int main()
    {
    B1 obj;
    obj.counts();
    obj.inp();
    cout<<"The total number of string is: "<<obj.total<<endl;

    }

    ReplyDelete
  4. #include
    using namespace std;
    class B
    {
    public:
    char a[10];
    int counts()
    {
    cout<< "Enter the string: ";
    cin.getline(a,10);
    int x, words = 0;

    for(int i = 0; a[i] != '\0'; i++)
    {
    if (a[i] == ' ')
    {
    words++;
    }
    }
    cout << "The number of string = " << words+1<< endl;
    return 0;
    }
    };
    class D: public B
    {
    public:
    char str[20];
    int i,total=0;
    void inp()
    {
    cout<<"Enter a character: ";
    cin>>str;


    for( i=0;str[i]!='\0';i++)
    {
    total++;

    }
    }

    };
    int main()
    {
    D obj;
    obj.counts();
    obj.inp();
    cout<<"The total number of word is: "<<obj.total<<endl;

    }

    ReplyDelete
  5. #include

    using namespace std;

    void display(char *);
    void display(string);

    int main()
    {
    string str1;
    char str[10];

    cout << "Enter a string: ";
    getline(cin, str1);

    cout << "Enter another string: ";
    cin.get(str, 10, '\n');

    display(str1);
    display(str);
    return 0;
    }

    void display(char s[])
    {
    cout << "Entered char array is: " << s << endl;
    }

    void display(string s)
    {
    cout << "Entered string is: " << s << endl;
    }

    ReplyDelete

Comment Here