C Program only!!! (Please not in C) Do Not Use Strings! Using Only Arrays, 1. Wr
ID: 3819843 • Letter: C
Question
C Program only!!! (Please not in C) Do Not Use Strings!
Using Only Arrays,
1. Write a program to remove an element from a character array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through the keyboard.
For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}.
Hint: Sequence of moving the element is important. You need to define a temp variable to hold the value of x[k] before you move the rest of the array elements one position back. Finally, put the saved temp variable to the first element of the array.
Explanation / Answer
#include <stdio.h>
int main()
{
int i,t,k; // variable initialisation
char ch[10];
printf("Enter the elements in the array "); //Entering the elements in the array
for(i=0;i<10;i++)
{
scanf("%s",&ch[i]);
}
printf("The elements are "); //printing the elements
for(i=0;i<10;i++)
{
printf("%c",ch[i]);
printf(" ");
}
printf("Enter the position you want change "); //user input for the position
scanf("%d",&k);
if(k>7)
{
printf("The given position is out of the array");
}
else
{
for(i=k;i>0;i--) //swapping the element from the user position
{
t=ch[i];
ch[i]=ch[i-1];
ch[i-1]=t;
}
}
printf("The elements are "); //printing the final array
for(i=0;i<10;i++)
{
printf("%c",ch[i]);
printf(" ");
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.