Thursday, March 18, 2021

, , ,

getline Function in c++

 Well as we know cin is an object which is used to take input from the user but does not allow to take the input in multiple lines.To accept the multiple lines as input we use getline c++

It is a pre-defined function in C++ which is defined in a <string.h> header file getline function c++ is used to  accept a line or a string from the input stream until the delimiting character is encountered by compiler.

 Syntax of getline() C++ function: 

this function can be represent in two way  :-

  • The first way of declaring function is to pass three parameters.
  1. istream& getline( istream& is, string& str, char delim ); 

the three parameter are  is, str, and delim.

Where :-

is: It is an object which is used from where to read the input stream.

str object: is used to stored a string.


delim: delimiting character.

Return value

getline function c++ returns the input stream object, which is passed as a parameter to the getline function.

  • The second mwthod of declaring is to pass two parameters.


istream& getline( istream& is, string& str ); 

as you can in above syntax we have two parameters whicch are is and str.

 this method does not have any delimiting character like first one.


is: It is an object which is used from where to read the input stream.

str object: is used to stored a string.

 Example of getline c++

 In this example we will take the input from user without using getline() function in C++

#include <iostream>   
#include  <string.h>
using namespace std;  
int main()  
{  
string name; // variable declaration  
std::cout << "Enter your name :" << std::endl;  
cin>>name;  
cout<<"\nHello "<<name;
return 0;  
}

OUTPUT 

getline c++
OUTPUT
As you see we give 'John Miller' as user input, but only 'John' was displayed in output. 

Let's resolve the above problem by using getline() C++ .

 #include <iostream>   
#include  <string.h> 
    using namespace std;  
    int main()  
    {  
    string name; // variable declaration.  
    std::cout << "Enter your name :" << std::endl;  
    getline(cin,name); // implementing a getline() function  
    cout<<"\nHello ""<<name;
return 0;  
}

In the above example we can get the full name with the help of getline function c++.

OUTPUT 

getline c++
OUTPUT getline function c++


Wednesday, March 17, 2021

, ,

fmod() function in C++

 fmod() function in C++ very useful for computes the floating point remainder of numerator/denominator (rounded towards zero).

Syntax for fmod() C++

fmod (x, y) = x - tquote * y

fmod() prototype

double fmod(double x, double y);
float fmod(float x, float y);
long double fmod(long double x, long double y);
double fmod(Type1 x, Type2 y); // Additional overloads for other combinations of arithmetic types

fmod() function in c++ takes two arguments and returns a value with the type of double, float or long double type.

fmod() C++ Parameters


    x as The value of numerator.
    y as The value of denominator.

Example 1 for How fmod() works in C++?


#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    double x = 7.5, y = 2.1;
    double result = fmod(x, y);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    
    x = -17.50, y = 2.0;
    result = fmod(x, y);
    cout << "Remainder of " << x << "/" << y << " = " << result << endl;
    
    return 0;
}

OUTPUT : - 

fmod c++ example
OUTPUT



Friday, March 12, 2021

, , , , ,

Friend function in C++ with example program

Well hiding the data is core of  object-oriented programming but friend function in C++ is an exception which break the rule of OOP's.

friend function and friend classes in C++ programming helps to give the access member functions from outside the class.

or in the other words the protected and private data of a class can be accessed with the help of function in C++.

Declare friend function in C++

 friend function can access the private and protected it should be declare with the the friend keyword.

 

class className {
    ... .. ...
    friend returnType functionName(arguments);
    ... .. ...
}

 Example program to understand Working of friend Function

 

// C++ program to demonstrate the working of friend function

#include <iostream>
using namespace std;

class Distance {
    private:
        int meter;
        
        // friend function
        friend int addFive(Distance);

    public:
        Distance() : meter(0) {}
        
};

// friend function definition
int addFive(Distance d) {

    //accessing private members from the friend function
    d.meter += 5;
    return d.meter;
}

int main() {
    Distance D;
    cout << "Distance: " << addFive(D);
    return 0;
}

As you can see here, addFive() in upper example is a friend function that can access both the private and public data members.

 friend Class in C++

 friend class can be used in c++ programming with friend keyword.

class ClassB;

class ClassA {
   // ClassB is a friend class of ClassA
   friend class ClassB;
   ... .. ...
}

class ClassB {
   ... .. ...
}

 if a class is declared a friend class in c++ programming then all the member functions of newly become friend class become friend functions.

See in the upper example  ClassB is a friend class so we can access all members of ClassA from inside ClassB.

 But we can't access members of ClassB from inside ClassA. 

Example program for C++ friend Class


// C++ program to demonstrate the working of friend class

#include <iostream>
using namespace std;

// forward declaration
class ClassB;

class ClassA {
    private:
        int numA;

        // friend class declaration
        friend class ClassB;

    public:
        // constructor to initialize numA to 12
        ClassA() : numA(12) {}
};

class ClassB {
    private:
        int numB;

    public:
        // constructor to initialize numB to 1
        ClassB() : numB(1) {}
    
    // member function to add numA
    // from ClassA and numB from ClassB
    int add() {
        ClassA objectA;
        return objectA.numA + numB;
    }
};

int main() {
    ClassB objectB;
    cout << "Sum: " << objectB.add();
    return 0;
}


Tuesday, March 9, 2021

, , , ,

Substring function in r programming

 Substring() function in R can be used to extract the characters present in the data or to manipulate the given data.

 You can easily extract the required characters from a string and  Substring() in r can also replace the values in a string.

Syntax for substring r :-

substr(x,start,stop)
substring(x,first,last=1000000L)

 Here we can do so many things with substring function in r link extracting of values, replacement of values etc.

    x =  input data / file.
    Start / First= starting index of the given substring.
    Stop / Last= Ending index of the substring.

r extract substring :-

#returns the characters from 1,11
df<-("computer_dev_private_limited")
substring(df,1,11)

OUTPUT : - 

"computer_de"

We hope you understand how to r extract part of the data from the given string.

r replace substring with program : -

#returns the string by replacing the _ by space
df<-("We are_developers")
substring(df,7,7)=" "
df


Output = “We are developers”

Put the same value to replace the values in a string with the help of
 Substring() function in R.

String replacement using substring() function with multiple value :-

#replaces the 4th letter of each string by $
df<-c("Alok","Joseph","Hayato","Kelly","Paloma","Moca")
substring(df,4,4)<-c("$")
df


Output = “Alo$” “Jos$ph” “Hay$to” “Kel$y” “Pal$ma” “Moc$”

Here the 4th letter in the given strings replaced by the "$" sign with the help of substring r.

Learn to use of str_sub() and substr() function in R :-

 well r programming is used to create the data in the form od row or colums.

With the help of given example you can learn to add data in existing data with the help of substr() function in R.

Example for substr() in r : -

#creates the data frame
df<-data.frame(Technologies=c("Datascience","machinelearning","Deeplearning","Artificalintelligence"),Popularity=c("70%","85%","90%","95%"))
df

OUTPUT : - 

OUTPUT
OUTPUT

Example for str_sub() function in R

#using the str_sub function
df$Extracted_Technologies=str_sub(df$Technologies,10,15)
> df

substring r
substr in r


Saturday, March 6, 2021

, , , , ,

Example program for isxdigit() function in c

 The isxdigit() function in c programming checks whether a character is a hexadecimal digit character (0-9, a-f, A-F) or not.

Syntax for isxdigit in c :-

int isxdigit( int arg );

 isxdigit() Parameters

The isxdigit() in c takes a single character as it's parameter.

 C isxdigit() Return Value

 if passed argument is an hexadecimal character then isxdigit() return non-zero integer otherwise return 0.

Example program for C isxdigit() function


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

int main() {
   char c = '5';
   int result;

   // hexadecimal character is passed
   result = isxdigit(c); // result is non-zero
   printf("Result when %c is passed to isxdigit(): %d", c, isxdigit(c));

   c = 'M';

   // non-hexadecimal character is passed
   result = isxdigit(c); // result is 0

   printf("\nResult when %c is passed to isxdigit(): %d", c, isxdigit(c));

   return 0;
}

OUTPUT : - 



C isxdigit()
C isxdigit()

, , ,

Example program for isspace() in c

 The isspace() function in c programming used to check whether a character is a white-space character or not.

It return the non-zero integer if given argument to the isspace() in c is a white-space character, otherwise return 0.

Syntax For isspace() function in c: -

int isspace(int argument);

First check out the List of all white-space characters in C programming are:

isspace in c
Check white-space character


Example program for Check white-space character


#include <stdio.h>
#include <ctype.h>
int main()
{
    char c;
    int result;

    printf("Enter a character: ");
    scanf("%c", &c);
    
    result = isspace(c);

    if (result == 0)
    {
        printf("Not a white-space character.");
    }
    else
    {
        printf("White-space character.");
    }

    return 0;
}


, , , , ,

Example program for ispunct in c

 The ispunct() function in c programming checks whether a character is a punctuation mark or not.

Syntax for ispunct() in c

int ispunct(int argument);

it returns a non-zero integer if given characters to ispunct() in c is an punctuation otherwise return 0.

Example for Program to check punctuation with the help of ispunct() function in c:-

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

int main() {
   char c;
   int result;

   c = ':';
   result = ispunct(c);

   if (result == 0) {
      printf("%c is not a punctuation", c);
   } else {
      printf("%c is a punctuation", c);
   }

   return 0;
}

 Output

ispunct
ispunct in c