putting character in the begginig and end of sring C

To put a character at the beginning and end of a string in C, you can follow these steps:

  1. Declare a character array to store the string. For example:
char str[100];
  1. Input or assign a string to the character array. For example:
strcpy(str, "Hello");
  1. Declare a character variable to store the character you want to add at the beginning and end of the string. For example:
char ch = '-';
  1. Calculate the length of the string using the strlen() function from the <string.h> library. For example:
int len = strlen(str);
  1. Shift the characters of the string to the right by one position to make room for the character at the beginning. Start from the last character and move towards the first character. For example:
for (int i = len; i >= 0; i--) {
    str[i + 1] = str[i];
}
  1. Assign the character to be added at the beginning to the first position of the string. For example:
str[0] = ch;
  1. Append the character at the end of the string by assigning it to the position after the last character. For example:
str[len + 1] = ch;
  1. Add a null character at the end of the modified string to indicate the end of the string. For example:
str[len + 2] = '\0';

Now, the str variable contains the modified string with the character added at the beginning and end.