I want to write a program that will take an input,determine it whether it is integer array or string. and then we will be abble to insert an elwment giving two parameters. 1. element, 2. position. I have done, given bellow but can't finish it. Is there anyone for me to help?

// insertion.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<stdlib.h>

int get_arr(char arr[])
{
    int i=0,isInt=0,j=0,IntArr[50];

    while(arr[i-1]!='\n')
    {
        scanf("%c",&arr[i]);
        i++;
    }

    if(arr[0]<58)
        continue;
    else
    {
        i=0;
        while(arr[i]!='\0')
        {
            if(arr[i]!=' ')
            {
                arr[i]=arr[i];
                i++;
            }
        }
        return isInt;
    }

    i=0;
    while(arr[i]!='\0')
    {
        while(arr[i]!=' ')
        {
            IntArr[j]=arr[i];
            i++;
        }
        arr[j]=atoi(IntArr);
        j++;
    }

    isInt=1;

    return isInt;
}

int arr_len(char arr[])
{
    int i=0;

    while(arr[i]!='\0')
        i++;

    return i;
}

int main()
{
    char arr[100],input[5];
    int i,len,pos=0,isInt=0;

    for(i=0; i<100;i++)
    {
        arr[i]=input[i]='\0';
    }

    printf("Input an array : ");
    isInt=get_arr(arr);

    len=arr_len(arr);

    printf("Taken array : ");
    for(i=0;i<len;i++)
        printf("%c",arr[i]);

    printf("Enter input element and position:\n");
    scanf("%s %d",&input,&pos);

    input=atoi(input);

    for(i=len;i>=pos-1;i--)
        arr[i]=arr[i-1];

    arr[pos-1]=input;

    printf("After insertion : ");

    for(i=0;i<len;i++)
        printf("%c",arr[i]);

    printf("\n");

    return 0;
}

Can you be more specific about what you can't complete? It's somewhat unreasonable to expect us to review your code and figure out what's missing from a brief description of the requirements.

getting the input. say, I've declared an array of characters. How will I store array of integers in that. please see get_arr() and its use in main()

Usually, I know before hand what the input will be - integer, string, double, etc.

If I don't know what will be next, I take the data in as a string, using fgets(), into a char array[80]. From there it can be checked with various tests, char by char, to see what the content of it should be.

Your accuracy depends on you knowing your data, very well.

You can perhaps use itoa() or atoi(), but they itoa functions are not standard C - might be in your compiler though, so check it out.

Purpose:
Converts an int, long int, or unsigned long int to a string.

Syntax:
char *_itoa(int value, char *dst, int base);

char *_ltoa(long int value, char *dst, int base);

char *_ultoa(unsigned long int value, char *dst, int base);

Declared in:
<stdlib.h>

Description:
The _itoa, _ltoa, and _ultoa function convertes, respectively, the int, long int, or unsigned long int in value to a character string in the array pointed to by dst (max 33 characters, including the terminating null character). The argument base is a value between 2 and 36 that specifies the desired number base. If base is 10 and value is negative, the first character in dst will be a minus sign.

My compiler adds an underline to the function name (prepends it). Yours may not do that.

Edited 3 Years Ago by Adak

thanks Adak. But say, i don't know what my data will be and it will be taken as keyboard input.

If a char is >='0' && the char is <= '9', then it must be a digit which is to be interpreted as a number - or at least a part of a number?

Unknown input I always take in as a string with fgets(). Then I know I have the input in my char array[], and I can test it as many different ways as is necessary to get it classified correctly.

This article has been dead for over six months. Start a new discussion instead.