Blog

  • C++ Program to Copy One String to Another

    We will write a C++ Program to Copy Strings. Before that, you may go through the following topics in C++.

    There are various ways to copy strings in C++. in this tutorial we will look at the following to copy strings.

    1. Using library function, strcpy()
    2. Without the use of strcpy() function

    C++ Program to Copy Strings

    The program takes user input for the string and then copy that string to another string in various ways shown below.

    1. C++ Program to Copy Strings Using strcpy() Function

    The strcpy() function takes two arguments, first where it should be copied and second what should be copied and returns the copied value.

    It is defined in string.h header file that needed to be included at the beginning of the program as shown below.

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
    
      cout << "Enter the string: ";
      cin >> str1;
    
      // copy the string
      strcpy(str2, str1);
    
      cout << "Copied String (str2): " << str2;
    
      return 0;
    }

    Output:

    Enter the string: simple2code
    Copied String (str2): simple2code


    2. C++ Program to Copy Strings without using strcpy() Function

    Here we have used for loop instead of the library function. Also, you can perform the same operation using a while loop too.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
      int i;
    
      cout << "Enter the string: ";
      cin >> str1;
    
      // copy the string
      for (i = 0; str1[i] != '\0'; ++i)
      {
        str2[i] = str1[i];
      }
    
      str2[i] = '\0';
    
      cout << "Copied String (str2): " << str2;
    
      return 0;
    }

    Output:

    Enter the string: John
    Copied String (str2): John


  • C++ Program to Compare Two Strings

    This is the C++ tutorial where we will write a C++ program to compare two strings. If you want to learn more about string in C++, click the link below.

    We will compare two strings in two ways:

    1. Compare using strcmp() Function
    2. Compare two strings without using strcmp() function

    C++ Program to Compare Two Strings

    Both of the programs below take user input for the two strings that are to compare. Although to take input from the user you can use gets(str) function that will take the string input with spaces in it.

    The following program uses cin>> method to take a string input. It only takes a single word.

    1. C++ Program to compare two strings without using strcmp()

    This program compare string in C++ without using strcmp() function. We sill use while loop to iterate through the string and inside while loop if loop is used to see if every letter is equal or not on both of the string.

    If it is not equal then the if statement is executed and a flag variable will raise to 1 and the execution control comes out of the loop with the use of break statement.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
      int i = 0, flag = 0;
    
      cout << "Enter the first string: ";
      cin >> str1;
      cout << "Enter the second string: ";
      cin >> str2;
    
      while (str1[i] != '\0' || str2[i] != '\0')
      {
        if (str1[i] != str2[i]) //if string are not equal
        {
          flag = 1;
          break;
        }
        i++;
      }
    
      if (flag == 0)
        cout << "\nStrings are Equal.";
      else
        cout << "\nStrings are not Equal.";
    
      return 0;
    }

    Output:

    Enter the first string: simple2code
    Enter the second string: simple2code

    Strings are Equal.


    2. C++ Program to compare two strings using strcmp()

    The following program compare string in C++ using strcmp() function that is provided in the C++ library. This inbuilt function is defined under string.h header file. Hence, it is necessary to include this file in the program at the beginning.

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
      char str1[50], str2[50];
    
      cout << "Enter the first string: ";
      cin >> str1;
      cout << "Enter the second string: ";
      cin >> str2;
    
      if (strcmp(str1, str2) == 0)
        cout << "\nStrings are Equal.";
      else
        cout << "\nStrings are not Equal.";
    
      return 0;
    }

    Output:

    Enter the first string: coding
    Enter the second string: coding

    Strings are Equal.

    Enter the first string: coding
    Enter the second string: Coding

    Strings are not Equal.

    As you can see, the comparison on both of the programs are also case sensitive, the word “coding” is not equal to the word “coding”. Since the first letter is a lower letter in one string and capital on another.


  • C++ Program to Find the Length of a String using Pointers

    This is the C++ tutorial where we will Write a C++ program to find length of string using pointer. If you want to learn more about Pointers and Strings in C++, click the link below.

    Explanation: The program takes the string value from the user as an input. we also declare a character pointer in a program. The address of the first character of a string is initialized to this pointer and then the length is calculated using while.

    You can also perform the same operation using for loop.

    • &: It is the address of operator.
    • *: It is the value at operator.

    C++ Program to Find the Length of a String using Pointers

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str[100], *ptr;
      int length = 0;
    
      cout << "Enter the String: ";
      cin >> str;
    
      //initializing the pointer
      ptr = &str[0];
    
      while(*ptr)
      {
         length++;
         ptr++;
      }
    
      cout << "\nLength of a string: " << length;
    
      return 0;
    }

    Output:

    Enter the String: simple2code

    Length of a string: 11

    However, if you want to enter the string with spaces, use gets(str) instead of cin >>str.

    Also, you may go through the following program on string:


  • C++ Program to Find the Length of a String

    This is the C++ tutorial where we will write a c++ program to find the length of a string using strlen() function. If you want to learn more about string in C++, click the link below.

    Explanation: The program takes the string value from the user as an input and then using one of the built-in functions strlen() present in C++ calculates the length of a string.

    Strlen(): This built-in function takes a string and returns the length of a string. In other words, returns the number of character present in the string. It is present in a header file named <string.h> which is included at the beginning of the code in a program.


    Find Length of String using strlen() Function in C++

    Source code:

    #include <iostream>
    #include <string.h>
    using namespace std;
    
    int main()
    {
      char str[100];
      int length = 0;
    
      cout << "Enter the String: ";
      cin >> str;
    
      length = strlen(str);
    
      cout << "\nLength of a string: " << length;
    
      return 0;
    }

    Output:

    Enter the String: simple2code

    Length of a string: 11

    However, if you want to enter the string with spaces, use gets(str) instead of cin >>str.

    Also, you may go through the following program:


  • C++ Program to Find the Length of a String without using strlen

    This is the C++ tutorial where we will write a C++ program to find the length of a string without using a library function or any built-in function such as strlen() function. If you want to learn more about string in C++, click the link below.

    Explanation: The program takes the string value from the user as an input and then using for loop or while loop, we can find the length of that string as shown in the program below.

    There is no use of library function to find the length, it is totally logic-based using loops in C++.


    C++ Program to Find the Length of a String without using strlen

    Source code: Find Length of String without strlen() Function in C++.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      char str[100];
      int length = 0, i = 0;
    
      cout << "Enter the String: ";
      cin >> str;
    
      while (str[i])
      {
        length++;
        i++;
      }
    
      cout << "\nLength of a string: " << length;
    
      return 0;
    }

    Output:

    Enter the String: simple2code

    Length of a string: 11

    You can also use for loop instead of while loop in the following manner.

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

    Replace the above code in place of the while loop then you will still get the same result. However, if you want to enter the string with spaces, use gets(str) instead of cin >>str.

    Also, you may go through the following program:


  • C++ Program to Print Prime numbers in a Given Range

    In this C++ programming example, we will write a C++ Program to display Prime Numbers between two intervals. Let us start by understanding what is a prime number.

    Prime Number:

    A Prime Number is a number that is only divisible by 1 and itself. Example: 2, 3, 5, 7, 11, 13, 17, etc. These numbers are only divisible by 1 and the number itself.

    Note:
    0 and 1 is not a prime number and 2 is the only even and smallest prime number.

    To understand the following program, you should have a basic idea about the following topics in C++ programming.


    C++ Program to Print Prime Numbers

    The program takes the user input for the starting number ( lower range) and the end number (upper range). Then prints all the prime numbers that fall within that range.

    Also notice in the program that 0 and 1 are specifically checked to make sure that those two are not printed even if the lower range entered by the user is 0.

    Print Prime Numbers in a Given Range in C++.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int lrRange, upRange, i, flag;
    
      cout << "Enter the lower range: ";
      cin >> lrRange;
      cout << "Enter the upper range: ";
      cin >> upRange;
    
      cout << "\nList of Prime numbers between " << lrRange << " & " << upRange << ": " << endl;
    
      while (lrRange < upRange)
      {
        flag = 0;
    
        // checking for 0 and 1
        if (lrRange == 0 || lrRange == 1)
        {
          flag = 1;
        }
        else
        {
          for (i = 2; i <= lrRange / 2; ++i)
          {
            if (lrRange % i == 0)
            {
              flag = 1;
              break;
            }
          }
        }
    
        if (flag == 0)
          cout << lrRange << " ";
    
        ++lrRange;
      }
    
      return 0;
    }

    Output:

    Enter the lower range: 0
    Enter the upper range: 30

    List of Prime numbers between 0 & 30:
    2 3 5 7 11 13 17 19 23 29

    You can also print the prime numbers between 0 to any range or 100 using the same logic as above. You may go through the following program for more.


  • C++ Program to Check Whether a Number is Prime or Not

    In this C++ programming example, we will learn about the prime number program in C++. We specifically write a program to check the entered number is prime or not. Let us start by understanding what is a prime number.

    Prime Number:

    A Prime Number is a number that is only divisible by 1 and itself. Example: 2, 3, 5, 7, 11, 13, 17, etc. These numbers are only divisible by 1 and the number itself.

    Note:
    0 and 1 is not a prime number and 2 is the only even and smallest prime number.

    To understand the following program, you should have a basic idea about the following topics in C++ programming.


    Check Prime Number in C++

    The program takes a user input for a number that needed to be checked for prime number. Since 0 and 1 are not prime numbers so we first check them using the if statement. And if the number is other than 0 and 1 the execution control goes to the else part of the statement.

    C++ Program to Check Whether a Number is Prime or Not

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int i, num, flag;
    
      cout << "Enter a positive integer: ";
      cin >> num;
    
      // checking for 0 and 1
      if (num == 0 || num == 1)
      {
        flag = 1;
      }
      else
      {
        for (i = 2; i <= num / 2; ++i)
        {
          if (num % i == 0)
          {
            flag = 1;
            break;
          }
        }
      }
    
      if (flag == 0)
        cout << num << " is a prime number";
      else
        cout << num << " is NOT a prime number";
    
      return 0;
    }

    Output:

    Enter a positive integer: 13
    13 is a prime number

    You may go through the following program of prime numbers in C++.


  • C++ Program to find ASCII Value of a Character

    In this tutorial, you will learn how to find ASCII Value of a Character in C++. Before that, you need to have knowledge of the following in C++ programming.

    ASCII stands for American Standard Code for Information Interchange. It is a 7-bit character set that contains 128 (0 to 127) characters. It represents the numerical value of a character.

    Example: ASCII value for character A is 65, B is 66 but a is 97, b is 98, and so on.


    C++ Program to find ASCII Value of a Character

    #include <iostream>
    using namespace std;
    
    int main() 
    {
     char ch;
     
     cout << "Enter a character: ";
     cin >> ch;
     
     cout << "ASCII Value of " << ch << ": " << int(ch);
     
     return 0;
    }

    Output:

    //Run 1
    Enter a character: q
    ASCII Value of q: 113

    //Run 2
    Enter a character: Q
    ASCII Value of Q: 81

    In the above program to print ASCII value in C++, there is an explicit conversion of character into an integer. When the explicit conversion is performed to convert into an integer, its ASCII value is printed.


  • Fibonacci series using Recursion in C++

    In this tutorial, we will write a Fibonacci Series program in C++ u. Before that, you should have knowledge of the following topic in C++.

    Fibonacci series is the series of numbers where the next number is achieved by the addition of the previous two numbers. The initial addition of two numbers is 0 and 1. For example: 0, 1, 1, 2, 3, 5, 8, 13….,etc.

    Recursion refers to the process when a function calls itself inside that function directly or indirectly or in a cycle.

    If you want to learn more about recursion in detail, click here. Although it is for C programming, the theory concept of recursion is the same for all programming languages.


    Fibonacci series using Recursion in C++

    #include <iostream>
    using namespace std;
    
    void recurFunc(int);  //function prototype
    
    int main()
    {
      int num;
    
      cout << "Enter the number of elements: ";
      cin >> num;
    
      cout << "Fibonacci Series: ";
      cout << "0 " << "1 ";
    
      //calling function
      //since two numbers are alredy printed so (n-2)
      recurFunc(num - 2);
    
      return 0;
    }
    
    void recurFunc(int n)
    {
      static int term1 = 0, term2 = 1, next;
    
      if (n > 0)
      {
        next = term1 + term2;
        term1 = term2;
        term2 = next;
    
        cout << next << " ";
    
        recurFunc(n - 1);
      }
    }

    Output:

    Enter the number of elements: 5
    Fibonacci Series: 0 1 1 2 3

    You may go through the following:


  • C++ Program to Display Fibonacci Series

    In this tutorial, we will write a Fibonacci Series program in C++. Before that, you should have knowledge of the following topic in C++.

    What is Fibonacci Series?

    Fibonacci series is the series of numbers where the next number is achieved by the addition of the previous two numbers. The initial addition of two numbers is 0 and 1.

    For example: 0,1,1,2,3,5,8,13….etc.

    We will learn two ways to display the Fibonacci series in C.

    1. upto n terms
    2. upto certain number.

    1. Fibonacci Series up to n Number of Terms in C++

    The program takes user input for the number terms to be displayed, for loop is used to iterate and print the number. Also, if statements are used for the first two numbers (0, 1).

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num, term1 = 0, term2 = 1, nextTerm = 0;
    
      cout << "Enter the number of terms: ";
      cin >> num;
    
      cout << "Fibonacci Series: ";
    
      for (int i = 1; i <= num; ++i)
      {
        // if is used for first two numbers in series
        if (i == 1)
        {
          cout << term1 << ", ";
          continue;
        }
    
        if (i == 2)
        {
          cout << term2 << ", ";
          continue;
        }
    
        nextTerm = term1 + term2;
        term1 = term2;
        term2 = nextTerm;
    
        cout << nextTerm << ", ";
      }
    
      return 0;
    }

    Output:

    Enter the number of terms: 5
    Fibonacci Series: 0, 1, 1, 2, 3,


    2. C++ Programs to display Fibonacci Series up to a certian number

    In this program, series is displayed up to an entered number. For example, if the user types 50 as an input then the program will display the series of terms present up to 50as shown below.

    #include <iostream>
    using namespace std;
    
    int main()
    {
      int num, term1 = 0, term2 = 1, next = 0;
    
      cout << "Enter a positive number: ";
      cin >> num;
    
      // for first two numbers
      cout << "Fibonacci Series: " << term1 << ", " << term2 << ", ";
    
      next = term1 + term2;
    
      while (next <= num)
      {
        cout << next << ", ";
    
        term1 = term2;
        term2 = next;
        next = term1 + term2;
      }
    
      return 0;
    }

    Output:

    Enter a positive number: 50
    Fibonacci Series: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

    You may go through the following: