Samiul_Islam_Abir

009

Jul 25th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. void insert_substring(char*, char*, int);
  6. char* substring(char*, int, int);
  7.  
  8. int main()
  9. {
  10.    char text[100], substring[100];
  11.    int position;
  12.  
  13.    printf("Given word\n");
  14.    gets(text);
  15.  
  16.    printf("Character to insert\n");
  17.    gets(substring);
  18.  
  19.    printf("At which position\n");
  20.    scanf("%d", &position);
  21.  
  22.    insert_substring(text, substring, position);
  23.  
  24.    printf("%s\n",text);
  25.  
  26.    return 0;
  27. }
  28.  
  29. void insert_substring(char *a, char *b, int position)
  30. {
  31.    char *f, *e;
  32.    int length;
  33.  
  34.    length = strlen(a);
  35.  
  36.    f = substring(a, 1, position - 1 );
  37.    e = substring(a, position, length-position+1);
  38.  
  39.    strcpy(a, "");
  40.    strcat(a, f);
  41.    free(f);
  42.    strcat(a, b);
  43.    strcat(a, e);
  44.    free(e);
  45. }
  46.  
  47. char *substring(char *string, int position, int length)
  48. {
  49.    char *pointer;
  50.    int c;
  51.  
  52.    pointer = malloc(length+1);
  53.  
  54.    if( pointer == NULL )
  55.        exit(EXIT_FAILURE);
  56.  
  57.    for( c = 0 ; c < length ; c++ )
  58.       *(pointer+c) = *((string+position-1)+c);
  59.  
  60.    *(pointer+c) = '\0';
  61.  
  62.    return pointer;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment