Jul 31, 2020

String in C Exercises & Solutions

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

C Program to Toggle Case of Each Character of a String

Ex: Write a C program to toggle case of each character of a string. How to write a C program to toggle case of each character of a string. C program to toggle case of each character of a string.

Input from user:

Enter the string:

welcome To Codeforhunger.Com

Expected output:

Toggled string:

WELCOME tO cODEFORHUNGER.cOM

Step by step logic of the given program:

1. Accept string from user declare varible say str.

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

3. Run for loop from 0 to length-1 of string, as follows:
for(i=0;i<len;i++)

4. Inside the body of loop check condition if character is lowercase alphabet then subtract 32 to make it capital and character is Capital alphabet then add 32 to make it small(lowercase), as follows:
if(str[i]>='a'&&str[i]<='z')
{
str[i]-=32;
}
else if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=32;
}

5. Last outside the block of for loop print toggled string.

Program to toggle case of each character of a string:

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

void main()
{
char str[100];
int i,len;

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

len=strlen(str);//calculate length of string

for(i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z')
{
str[i]-=32;//subtract 32 to make it capital
}
else if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=32;//add 32 to make it small
}
}

printf("Toggled string:\n%s",str);
}

Above program shows the following output:

C program to print given string in reverse order without using strrev()

Ex: Write a C program to print given string in reverse order without using strrev(). How to write a C program to print given string in reverse order without using strrev(). C program to print given string in reverse order without using strrev().

Input from user:

Enter the string:
codeforhunger.com

Expected output:

Given string in reverse order:
moc.regnuhrofedoc

Step by step logic of the given program:

1. Accept string from user declare varible say str.

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

3. Run for loop from length of string to 0 which is looks like:
for(i=len;i>=0;i--)

4. Inside the for loop print string characters one by one.

Program to print given string in reverse order without using strrev():

#include<stdio.h>
#include<string.h>
#define max 100

void main()
{
char str[max];
int i,len=0;

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

len=strlen(str);

printf("Given string in reverse order: \n");
/*print given string in reverse order*/
for(i=len;i>=0;i--)
{
printf("%c",str[i]);
}
}

Above program shows the following output:

C program to copy one string to another string without using strcpy() function

Ex: Write a C program to copy one string to another string without using strcpy() function.
How to copy one string to another string without using strcpy() function. C program to copy one string to another string without using strcpy() function.

Input from user:

Enter the string:
I love codeforhunger

Expected output:

First string: I love codeforhunger

Second string: I love codeforhunger

Step by step logic of the given program:

1. Accept string from user declare varible say str1.

2. Declare another variable to store copy of first string, varible say str2.

3. Run while loop from 0 to end of string.

4. Inside the while loop for each character in str1 copy to str2 using:
str2[i]=str1[i];

5. After loop make sure the copied string ends with NULL character using:
str2[i]= '\0';

Program to copy one string to another string without using strcpy() function:

#include<stdio.h>
#include<string.h>
#define max 100

void main()
{
char str1[max],str2[max];//declare string of max size 100
int i=0;

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

/*Iterate loop till end of string*/
while(str1[i]!='\0')
{
/*copy the string */
str2[i]=str1[i];
i++;
}
/*make sure that string is Null terminated*/
str2[i]='\0';

printf("\nFirst string: %s",str2);
printf("\nSecond string: %s",str2);
}

Above program shows the following output:

C Program to count total number of words in a string

Ex: Write a C program to count total number of words in a string. How to  count total number of words in a string. C Program to count total number of words in a string.

Input from user:

Enter the string:
This is codeforhunger.com

Expected output:

Number of words are: 3

Step by step logic of the given program:

1. Accept string from user declare varible say str.

2. Run while loop till end of the string.

3. Inside the while loop check whether the current character is space or new line using:
if(str[i]== ' '||str[i]=='\n')
{
count++;
}

4. After that Print value of  count on the output screen.

Program to count total number of words in a string:

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

void main()
{
char str[100];//declare string of max size 100
int i=0,count=1;

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

/*Iterate loop till end of string*/ while(str[i]!='\0')
{
/* check whether the current character is space or new line*/
if(str[i]== ' '||str[i]=='\n')
{
count++;
}
i++;
}
printf("Number of words are:%d",count);
}

Above program shows the following output:

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.com

Expected output:

String characters are:

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

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:

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 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:

Jul 25, 2020

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 null ('/0') character.

Syntax:

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

How it works?

char str[]="Hello";

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:

Jul 21, 2020

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

Logic to multiply two matrices:

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

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:

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

Jul 18, 2020

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

• Logic of matrix Subtraction:

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

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:

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

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

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

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:

Jul 16, 2020

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

Jul 14, 2020

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

C Program to Print Array Elements in Reverse Order

Ex: Write a C program to print given array elements in reverse order. How to write a C program to print array elements in reverse order. C program to print array elements in reverse order.

Input from user:

Enter the limit: 5

Enter the elements:
10
20
30
40
50

Expected output:

Given elements in reverse order:
50 40 30 20 10

What is Break Statement in C

Break statement is very useful to exit from any loop  such as for loop, while loop and do while loop. When the break statement is encountered in the loop then the loop will stop running the statements and immediately exit from the loop.

Jul 13, 2020

Continue Statement in C

Continue statement skips some lines of code inside the loop and continues with the next iteration.
When the continue statement is encounters in a loop, then program will skip the all the statements after the continue statement and the loop continues with the next iteration.
It is mainly used for a code which we want to skip.

Jul 11, 2020

C Program to Capitalize the Given String

Ex: Write a C program to capitalize a given string. How to write a C program to capitalize given string. C program to capitalize given string.

Input from user:

Enter the string:
tHis IS cOdeForhuNger

Expected output:

String after capitalize:
This Is Codeforhunger

Jul 7, 2020

C Program to Find Largest Element of Array

Ex: Write a C program to print largest element of array. How to write a C program to find largest element of array. C program to print largest element of array.

Input from user:

Enter the limit: 12

Enter elements:

12

34
-1
54
7
9
16
44
32
21
19

Jul 6, 2020

C Program to print Total number of Notes in given Amount

Ex: Write a C program to find total number of notes in given amount. How to write a C program to print total number of notes in given amount. C program to print total number of notes in given amount.

Input from user:

Enter the amount: 2528

Expected output:

500=5

100=0
50=0
20=1
10=0
5=1
2=1
1=1

C Program to Find Percentage of given Subjects Marks using Array

Ex: Write a C program to find percentage of given subjects marks using array. How to write a C program to print percentages of given marks. C program to find percentage of given subjects marks.

Input from user:

Enter limit of subjects: 5

Enter marks of subject[1]:71

Enter marks of subject[2]:65
Enter marks of subject[3]:61
Enter marks of subject[4]:81
Enter marks of subject[5]:77

Expected output:

Percentage= 71.0000