C program to separate the individual characters from a string

Ex: Write a C program to separate the individual characters from a string. How to write a C program to separate the individual characters from a string. C program to separate the individual characters from a string.

Input from user:


Enter the string:

codeforhunger

Expected output:


String characters are:

c o d e f o r h u n g e r





  Step by step logic of the given program:


1. Accept string from user declare variable say str.

2. Run while loop till end of string.

3. Print characters of string one by one using spaces.




  Program to separate the individual characters from a string :


#include<stdio.h>
#include<string.h>

int main()
{
char str[100];//declare string of size 100
int i=0;
printf("Enter the string:\n");
gets(str);//accept string
printf("String characters are:\n");
while(str[i]!='\0')
{
printf("%c ",str[i]);
i++;
}
return 0;
}


Above program shows the following output:

Enter the string:         
codeforhunger            
String characters are:  
c o d e f o r h u n g e r 


To learn more string exercises click here...

C program to calculate length of string using strlen() function

Ex: Write a C program to calculate length of string using strlen() function. How to calculate length of string using strlen() function. C program to calculate length of string using strlen() function.

Input from user:


Enter the string:
This is codeforhunger

Expected output:

Length of string is: 21





  Step by step logic of the given program:

1. Accept string from user store it in variable say a.


2. After that calculate length of string using strlen() function and store it in variable length.

3. Print length on the output screen.




  Program to calculate length of string using strlen() function:



#include<stdio.h>
#include<string.h>

int main()
{
char a[100];
int length;

printf("Enter the string:\n");
    gets(a);
    
    length=strlen(a);//calculate length of string

printf("Length of string is: %d",length);

return 0;
}



Above program shows the following output:


C program to calculate length of string using strlen() function

C program to accept string from user & print it on the output screen

Ex: Write a C program to accept string from user and print it on the output screen. How to accept string from user and print it on the output screen. C program to accept string from user and print it on the output screen.

Input from user:

Enter the string:
I love codeforhunger

Expected output:

You entered:
I love codeforhunger






  Step by step logic of the given program:

1. Declare string of size 100, declare variable say str.

2. Accept string using gets() function and store it in variable str.

3. Print string on the output screen.




  Program to accept string from user and print it on the output screen:


#include<stdio.h>
#include<string.h>

void main()
{
char str[100];

printf("Enter the string:\n");
 gets(str);

printf("you entered: %s",str);
}



Above program shows the following output:


C program to accept string from user and print it on the output screen

String in C Definition, Syntax and Working

Strings are mainly defined as an array of characters. In simple words it is one dimensional array of characters which is terminated by special character called the null ('/0') character. 

In simple words string is a line of characters, like a word or a sentence. Each character in the string is stored in a specific memory location, and the null character marks the end of the string.



  Syntax:

datatype array_name[size of array];
{
       //block of code
}




  How it works?

char str[]="Hello";

String in C  definition, syntax and working, String in C programming


Above image shows the memory representation of string with their index and memory address.




  Example program of string:



#include<stdio.h>
void main()
{
char str[]="Hello";

printf("%s",str);
}


Above program shows the following output:

String in C  definition, syntax and working, Strings in C programming
 

To explore the exercises and examples of String click Here...

C Program to Print Multiplication of Two Matrices using Array

Ex: Write a C program to print multiplication of two matrices. How to write a C program to print multiplication of two matrices. C program to print multiplication of two matrices.

Input from user:

Enter elements in first matrix of size 3x3:
1 3 5
2 4 6
3 5 7

Enter elements in second matrix of size 3x3: 
1 3 5
2 4 6
3 5 7

Expected output:

Multiplication of two matrices=
22 40 58
28 52 76
34 64 94



  • Quick links:
  1. What is array?
  2. For loop



   Logic to multiply two matrices:


To print multiplication of two matrices we need to multiply it into below(row*column) format:


C program to print multiplication of two matrices using array
C program to print multiplication of two matrices using array




  Step by step logic of the given program:



1. Accept two matrices from user declare varible say a and b(use 2 dimensional array to store elements).

2. Store elements one by one in matrix a using: 

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
            scanf("%d", &a[row][col]);
        }
    }
Follow same these  steps for matrix b.

3. Multiply two matrices a&b element wise using:

for(i=0;i<size;i++)
{
sum+=a[row][i]*b[i][col];

}

4. Print multiplication of two matrices on the output screen.




  Program to print multiplication of two matrices using array:


#include<stdio.h>
#define size 3//size of matrix

int main()
{
int a[size][size],
b[size][size],
c[size][size];

int row,col,i,sum=0;

//accept elements in first matrix
printf("Enter elements in first matrix of size 3x3:\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d",&a[row][col]);
}
}

//accept elements in second matrix
printf("Enter elements in second matrix of size 3x3:\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d",&b[row][col]);
}
}
//multiply both matrices a&b
for(row=0;row<size;row++)
{
     for(col=0;col<size;col++)
     {
    for(i=0;i<size;i++)
{
sum+=a[row][i]*b[i][col];
}
c[row][col]=sum;
sum=0;
  }
}

//print multiplication of matrices
printf("Multiplication of matrices=\n");
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
printf("%d  ",c[row][col]);
}   
  printf("\n");
}
return 0;
}


Above program shows the following output:


C program to print multiplication of two matrices using array


Share your thoughts below in the comment section....

C Program to Print Subtraction of Two Matrices using Array

Ex: Write a C program to subtract two matrices using array. How to write a C program to subtract two matrices using array. C program to subtract two matrices using array.

Input from user: 


Enter elements in 1st matrix of size 3x3: 

1 2 3
4 5 6
7 8 9

Enter elements in 2nd matrix of size 3x3:

1 2 3
4 5 6
7 8 9


Expected output:


Subtraction of matrices=

0 0 0
0 0 0
0 0 0



  • Quick links:
  1. What is array?
  2. For loop


  • Logic of matrix Subtraction:

Matrix Subtraction is done element wise. Means Subtraction of matrices is defined by A-B= A↓ij - B↓ij.

C program to print subtraction of two matrices using array
C program to print subtraction of two matrices using array




  Step by Step logic of the given program:


1. Accept two matrices from user declare varible say arr1 and arr2(use 2 dimensional array to store elements).

2. Store elements one by one in arr1 using: 

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
            scanf("%d", &arr1[row][col]);
        }
    }
Follow same these  steps for arr2.

3. Subtract two matrices arr1-arr2 element wise using:

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
          arr3[row][col] = arr1[row][col] - arr2[row][col];
        }
    }

4. Last print Subtraction of two matrices means resultant matrix arr3.





  C program to print subtraction of two matrices using array:


#include<stdio.h>
#define size 3 // size of the matrix

int main()
{

    int arr1[size][size]; // 1st matrix
int arr2[size][size]; // 2nd Matrix
int arr3[size][size]; // Resultant matrix


    int row, col;

/* input elements in first matrix*/
printf("Enter elements in 1st matrix of size 3x3:\n");

    for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr1[row][col]);
}
}

/* input elements in second matrix */

    printf("\nEnter elements in 2nd matrix  of size 3x3: \n");

    for(row=0; row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr2[row][col]);
}
}


    /* subtract both matrices arr1 and arr2 and store result in matrix arr3*/
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
    {
  arr3[row][col] = arr1[row][col] - arr2[row][col];
}
}


 /* Print the value of resultant matrix arr3 */ 
printf("\nSubtraction of matrices = \n");

    for(row=0; row<size;row++)
{
for(col=0; col<size;col++)
{
printf("%d ",arr3[row][col]);
}
printf("\n");
}

    return 0;

}



Above program shows the following output:


C program to print subtraction of two matrices using array


Thanks for visiting. If you have any doubts, Feel free to share in the comment section. 
To Explore more C Programming Examples Click Here.

C Program to Add Two Matrices using Array

Ex: Write a C program to add two matrices using array. How to write a C program to add two matrices using array. C program to add two matrices using array.

Input from user: 

Enter elements in 1st matrix of size 3x3: 

1 1 1
2 2 2
3 3 3

Enter elements in 2nd matrix of size 3x3:

4 4 4
3 3 3
2 2 2


Expected output:


Sum of matrices=

5 5 5
5 5 5
5 5 5



  • Quick links:
  1. What is array?
  2. For loop



  • Logic of matrix addition:

Matrix addition is done element wise. Means sum of matrices is defined by A+B= A↓ij + B↓ij.



C program to add two matrices using array,  Matrix addition using C programming language
C program to add two matrices using array




  Step by Step logic of the given program:


1. Accept two matrices from user declare varible say arr1 and arr2(use 2 dimensional array to store elements).


2. Store elements one by one in arr1 using: 

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
            scanf("%d", &arr1[row][col]);
        }
    }
Follow same these  steps for arr2.

3. add two matrices arr1+arr2 element wise using:

for(row=0;row<size;row++)
    {
        for(col=0;col<size;col++)
        {
          arr3[row][col] = arr1[row][col] + arr2[row][col];
        }
    }

4. Last print sum of two matrices means resultant matrix arr3.






  C program to add two matrices using array:


#include<stdio.h>
#define size 3 // size of the matrix

int main()
{

    int arr1[size][size]; // 1st matrix
int arr2[size][size]; // 2nd Matrix
int arr3[size][size]; // Resultant matrix


    int row, col;

/* input elements in first matrix*/
printf("Enter elements in 1st matrix of size 3x3:\n");

    for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr1[row][col]);
}
}

/* input elements in second matrix */

    printf("\nEnter elements in 2nd matrix  of size 3x3: \n");

    for(row=0; row<size;row++)
{
for(col=0;col<size;col++)
{
scanf("%d", &arr2[row][col]);
}
}


    /* Add two matrices arr1 and arr2 and store result in matrix arr3*/
for(row=0;row<size;row++)
{
for(col=0;col<size;col++)
    {
  arr3[row][col] = arr1[row][col] + arr2[row][col];
}
}


 /* Print the value of resultant matrix arr3 */ 
    printf("\nSum of matrices = \n");

    for(row=0; row<size;row++)
{
for(col=0; col<size;col++)
{
printf("%d ",arr3[row][col]);
}
printf("\n");
}

    return 0;

}


Above program shows the following output:


C program to add two matrices using array

Array's in C Exercises and Solutions

Array is collection of similar data types. You can store group of data of same data types using array. In array different data types elements are not allowed that simply means if we declare an integer array then we can store only integer type of elements in it.


There are two types of arrays in C:

1. One dimensional array.

2. Multi-dimensional array:

  • Two dimensional array.
  • Three dimensional array.
  • Four dimensional array etc...

C Program to Merge Two Arrays Sorted in Descending Order

Ex: Write a C program to merge two arrays and sort in descending order. How to write a C program to merge two arrays and sort in descending order. C program to merge two arrays and sort in descending order.

Input from user:

Enter the limit of first array: 3
Enter elements: 
1
2
3

Enter the limit of second array: 3
Enter elements:
1
2
3

C Program to Copy the Elements of one Array into Another Array

Ex: Write a C program to copy the elements of one array into another array. How to write a C program to copy the elements of one array into another array. C program to copy the elements of one array into another array.


Input from user:

Enter the limit: 5

Enter elements: 
1
3
5
7
9

Expected output:

Elements in 1st array are: 
arr[0]=1
arr[1]=3
arr[2]=5
arr[3]=7
arr[4]=9