Strings & Arrays

This is a discussion on Strings & Arrays within the C Programming forums, part of the General Programming Boards category; Hello, I need a program that 's able to place a character anywere in a string. This is what Iv ...

  1. #1
    Unregistered
    Guest

    Unhappy Strings & Arrays

    Hello,
    I need a program that 's able to place a character anywere in a string. This is what Iv got so far.

    #include <stdio.h>
    main()
    {
    void strins(char string[], char character[], int position);
    char string[30];
    char str_ing;
    int i = 0;
    char character[1];
    int position;

    printf("Enter a string\n");


    do
    {
    str_ing = getchar();
    string[i] = str_ing;
    i++;
    } while(str_ing != '\n');
    string[i-1] = '\0';
    printf("the letter and postion of correction\n");
    scanf("%s %d",character,&position);

    strins(string,character,position);
    }

    void strins(char string[], char character[], int position)
    {
    int i;


    for (i=0, j=0;string[i] == character[position]; ++i, j++ )
    string[i] = character[position] + string[i];

    puts(string);


    }
    IT's in the function im getting confused, Thanks for any help.

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Try this, i changed a few things mainly i removed scanf and replaced it with fgets and sscanf, they're safer to use.
    I also used pointers as they are less messy than a load of square brackets, enjoy.
    Code:
    #include <stdio.h>
    
    void strins(char *str_ptr, char ch, int pos);
    
    int main()
    {
        char string[31], temp[5], ch;
        int pos;
    
        printf("\nEnter a string ");
        fgets(string, sizeof(string) - 1, stdin);
    
        printf("\nEnter letter and position of correction ");
        fgets(temp, sizeof(temp) - 1, stdin);
        sscanf(temp, "%c %d", &ch, &pos);
    
        strins(string, ch, pos);
    
        return 0;
    }
    
    void strins(char *str_ptr, char ch, int pos)
    {
        str_ptr += (pos - 1);
        *str_ptr = ch;
        str_ptr -= (pos - 1);  /* reset pointer */
        puts(str_ptr);
    
        return;
    }
    Last edited by C_Coder; 12-12-2001 at 03:31 PM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 07:37 AM
  2. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  3. working with strings arrays and pointers
    By Nutka in forum C Programming
    Replies: 4
    Last Post: 10-30-2002, 08:32 PM
  4. strings or character arrays
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 07-21-2002, 11:55 AM
  5. Searching arrays for strings
    By Zaarin in forum C++ Programming
    Replies: 14
    Last Post: 09-03-2001, 07:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21