Tuesday, 12 April 2016

C And C++ Program to find length of a string Using library function Problem Solution.

C Solution:

#include <stdio.h>
#include <string.h>
 /*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    char *str;

    str = (char*) malloc (sizeof(char));
    printf("Enter any string: ");
    gets(str);

    printf("Length of \"%s\" = %d.", str, strlen(str));

    return 0;
}

C++ Solution:

#include <iostream>
#include<string>
using namespace std;
 /*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
int main(){

    string str;

    cout << "Enter any string: ";
    getline(cin,str);

    cout << "Length of \"" << str << "\" = " << str.length() << endl;

    return 0;
}

No comments:

Post a Comment