Add prefix or suffix to each element in a list in C++
In this tutorial, we will learn how to add prefix or suffix to each element in a list in C++. Before proceeding for implementation let’s first understand what do you mean by prefix and suffix.
Prefix: is an affix that is placed at the beginning of an entity in order to alter its meaning.
For example in the word “rematch” re is a prefix and match is an original entity.

Suffix: is an affix that is placed at the end of an entity in order to alter its meaning.
For example in the word “strongest” est is a suffix and strong is an original entity.

Program to add prefix or suffix to each element in a list in C++
#include<iostream>
#define MAX 100
using namespace std;
void add_prefix(int arr[], int n, int prefix)
{
/* result will stored in array 'temp' */
int temp[MAX],cnt=1;
/* after adding prefix to each element size of resultant array will become 2n and loop will execute for index 0 to 2n-1 */
for(int i=0 ; i<= 2*n-1 ; i++)
{
/* prefix will occupy even indices of the resultant array */
if(i%2==0)
{
temp[i]=prefix;
}
else
{
/* element which was at index 'i-cnt' in original array will occupy index 'i' of the resultant array. */
temp[i]=arr[i-cnt];
cnt++;
}
}
cout<<"\nResultant array after adding prefix to each element in an array: ";
for(int i=0;i<=2*n-1;i++)
{
cout<<temp[i]<<" ";
}
}
void add_suffix(int arr[], int n, int suffix)
{
/* result will stored in array 'temp' */
int temp[MAX];
/* after adding suffix to each element size of resultant array will become 2n and loop will execute for index 0 to 2n-1 */
for(int i=0 ; i<= 2*n-1 ; i++)
{
/* suffix will occupy odd indices of the resultant array */
if(i%2==1)
{
temp[i]=suffix;
}
else
{
/* element which was at index 'i/2' in original array will occupy index 'i' of the resultant array. */
temp[i]=arr[i/2];
}
}
cout<<"\nResultant array after adding suffix to each element in an array: ";
for(int i=0;i<=2*n-1;i++)
{
cout<<temp[i]<<" ";
}
}
int main()
{
int n, ch, arr[MAX],i,prefix,suffix;
cout<<"Enter no. of elements of the list: ";
cin>>n;
cout<<"\nEnter elements of the list: ";
for(i=0;i<n;i++)
{
cin>>arr[i];
}
cout<<"\n****menu****";
cout<<"\n1) Add prefix to each element in a list";
cout<<"\n2) Add suffix to each element in a list";
cout<<"\nEnter your choice (1 or 2): ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter prefix: ";
cin>>prefix;
add_prefix(arr,n,prefix);
break;
case 2:
cout<<"\nEnter suffix: ";
cin>>suffix;
add_suffix(arr,n,suffix);
break;
/* Writing default case is not compulsory but it is a good programming practice. We can write default case anywhere in the switch block */
default:
cout<<"\nInvalid choice..!!";
}
return 0;
}Input/Output (prefix):
Enter no. of elements of the list: 4 Enter elements of the list: 1 2 3 4 ****menu**** 1) Add prefix to each element in a list 2) Add suffix to each element in a list Enter your choice (1 or 2): 1 Enter prefix: 0 Resultant array after adding prefix to each element in an array: 0 1 0 2 0 3 0 4
Input/Output (suffix):
Enter no. of elements of the list: 4 Enter elements of the list: 1 2 3 4 ****menu**** 1) Add prefix to each element in a list 2) Add suffix to each element in a list Enter your choice (1 or 2): 2 Enter suffix: 0 Resultant array after adding suffix to each element in an array: 1 0 2 0 3 0 4 0
You may also read:
Leave a Reply