Hi everyone!
This is my first post and i'm also a newbie so don't be harsh on me!
I have this exercise to do which asks pretty much what the title says.
I have a function that reads strings from a file and puts them in an array. Then I insert from the keyboard a new string to the same array and I have a second function where it tries to sort them alphabetically.
I managed to write down a version of the code, using bits and pieces, without really understanding what I'm writing. Here it is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
FILE *file;
int ReadStrings(char filename[], char A[], int size);
int InsertStrings (char num[], char A[], int size, int noElements);
int main(void)
{
int c, i, n, d, result;
char str[100], name[100], m[100];
printf("Please enter the file's name:");
scanf ("%s",name);
do
{
printf("\nPlease enter the size of the array (no bigger than 100):");
scanf("%d",&n);
}
while (n>100);
result=ReadStrings(name,str,n);
printf("\nThe number of strings inserted in the array are %d\n",result);
for (i=result;i<=n-1;i++){
printf("\nEnter new string to sort in the array, -1 to end: ");
scanf("%s",m);
d=InsertStrings(m, str, n, result);
result++;
if (d==0)
printf("\n0 - No number was inserted\n");
else if (d==1)
printf("\n1 - One number was inserted\n");
}
system ("pause");
return 0;
}
int ReadStrings(char filename[], char A[], int size)
{
int i=0;
if ((file = fopen (filename, "r" ) ) != NULL )
{
while (!feof (file)&&(i<size))
{
fscanf(file,"%s",&A[i]);
printf("%s ",&A[i]);
i++;
if (i == size)
{
printf("\n0 - No number was inserted\n");
break;
}
}
}
else
{
printf("\n Error at File Open \n");
}
return i ;
}
int InsertStrings(char num[],char A[], int size, int noElements)
{
int i=0,L=1,m,j,k=0,pos, d=1;;
char B[100]={},C[100]={};
for(i=0;i<size;i++)
B[i]=A[i];
printf("You have entered %s\n",num);
i=-1;
while(i<size)
{
i++;
if(B[i]==' ')
{
k=0;
m=strcmp(num,C);
if(m<=0)
{
k=0;
for(j=0;j<size;j++)
C[j]=' ';
break;
}
else
{
k=0;
L++;
if((B[i]==' ')&&(B[i+1]==' '))
break;
for(j=0;j<size;j++)
C[j]=' ';
}
}
else if(B[i]!=' ')
{
C[k]=A[i];
k++;
}
}
printf("''%s'' will be placed in array spot %d\n",num,L);
if(L==1)
{
for(i=0;i<size;i++)
A[i]=' ';
for(i=0;i<(strlen(num));i++)
A[i]=num[i];
k=0;
for(i=(strlen(num)+1);i<size;i++)
{
A[i]=B[k];
k++;
}
printf("\n");
for(i=0;i<size;i++)
printf("%s",&A[i]);
}
else if(L>1&&L<=noElements)
{
k=0;
for(i=0;i<size;i++)
{
if(B[i]==' ')
{
k++;
pos=i;
if(k==L-1)
break;
}
}
j=0;
for(i=(pos+1);i<((pos+1)+(strlen(num))+1);i++)
{
A[i]=num[j];
j++;
}
j=pos+1;
for(i=((pos+1)+(strlen(num))+1);i<size;i++)
{
A[i]=B[j];
j++;
}
printf("\n");
for(i=0;i<size;i++)
printf("%s",&A[i]);
}
else if(L>noElements)
{
k=0;
while(k!=noElements)
{
for(i=0;i<size;i++)
{
if(B[i]==' ')
{
k++;
pos=i;
if(k==L-1)
break;
}
}
k=0;
for(i=(pos+1);i<( (pos+1)+(strlen(num)));i++)
{
A[i]=num[k];
k++;
}
printf("\n");
for(i=0;i<size;i++)
printf("%s",&A[i]);
}
return d;
}
}
Can you help me out to what I want? The program seems to be working fine up to a certain point but then all hell's breaking loose! Thnx in advance!