-
Help ignoring spaces.
i have writen a program which basically does a form of SSC. it loads two arrays and then prints them.
then takes a user input and changes the a's to d's (just an example), but i want to make it ignore the spaces a user may enter and just push the letter together.
so if a user enters "AND THE", i want the out put to be "ANDTHE", but at the mo im just getting "AND", really struglling with it. ive tryed a few things but with no sucsses.
Also just a little thing. does anyone no how to allow a user to enter a file name. then a file ive already writen to be put into to it and then writen.
Thanks everyone, any help welcome, heres my code.
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_BUF 27
void main ()
{
FILE *file = fopen( "key.txt", "r" );
char input[25]; //input
char array[25]; //cypher text
char i;
char alp[25]; //alphabet
printf("This program will take Capital letters only and code them\n");
if ( file == 0 )
{
printf( "Could not open file\n" );
}
else
{
int x;
int num = 0;
while ( ( x = fgetc( file ) ) != EOF )
{
if( num < (MAX_BUF-1) ) {
array[num++] = x;
}
}
array[26] = '\0';
printf("Cypher Text: \n%s", array );
printf("\n");
}
FILE *fp = fopen( "alpha.txt", "r" );
if ( fp == 0 )
{
printf( "Could not open file\n" );
}
else
{
int x;
int num = 0;
while ( ( x = fgetc( fp ) ) != EOF )
{
if( num < (MAX_BUF-1) ) {
alp[num++] = x;
}
}
alp[26] = '\0';
printf("Alphabet: \n%s", alp);
printf("\n");
}
printf("\nPlease enter a line of text to be coded: ");
scanf("%s",&input);
printf("\n");
for (i = 0; i < strlen(input);++i)
{
input[i] = toupper (input[i]);
}
printf("Your text in upper case:%s\n",input);
// MAKE A TEXT THAT SAYS ALL OTHER SYMBOLS WILL BE REMOVED IN "THE NAME A USER ENTERED"
for (i = 0; i < strlen(input);++i){
if (input[i] == alp[0])
input[i] = array[0];
else if(input[i] == alp[1])
input[i] = array[1];
else if(input[i] == alp[2])
input[i] = array[2];
else if(input[i] == alp[3])
input[i] = array[3];
else if(input[i] == alp[4])
input[i] = array[4];
else if(input[i] == alp[5])
input[i] = array[5];
else if(input[i] == alp[6])
input[i] = array[6];
else if(input[i] == alp[7])
input[i] = array[7];
else if (input[i] == alp[8])
input[i] = array[8];
else if (input[i] == alp[9])
input[i] = array[9];
else if (input[i] == alp[10])
input[i] = array[10];
else if (input[i] == alp[11])
input[i] = array[11];
else if (input[i] == alp[12])
input[i] = array[12];
else if (input[i] == alp[13])
input[i] = array[13];
else if (input[i] == alp[14])
input[i] = array[14];
else if (input[i] == alp[15])
input[i] = array[15];
else if (input[i] == alp[16])
input[i] = array[16];
else if (input[i] == alp[17])
input[i] = array[17];
else if (input[i] == alp[18])
input[i] = array[18];
else if (input[i] == alp[19])
input[i] = array[19];
else if (input[i] == alp[20])
input[i] = array[20];
else if (input[i] == alp[21])
input[i] = array[21];
else if (input[i] == alp[22])
input[i] = array[22];
else if (input[i] == alp[23])
input[i] = array[23];
else if (input[i] == alp[24])
input[i] = array[24];
else if (input[i] == alp[25])
input[i] = array[25];
else if (90 >input[i] < 65)
input[i] = ' ';
input[i] = input[+i];
}
input[26] = '\0';
FILE *out;
out = fopen("out.txt","w");
fprintf(out,"%s",input); //putting coded text in to out.txt
fclose( file );
}
any help welcome
-
1. int main
2. if fopen failed - do not continue, print the error and exit
return -1; to indicate the error (that's why main should return the int - see FAQ)
3. if you have MAX_BUF define - use it - instead of magical number 25
currently array[26] is out of bounds access, event array[25] is out of bounds
if you declare
char array[SIZE] valid indexes are from 0 to SIZE-1
4. in your loop reading chars - check the current index to prevent buffer overrun
Code:
if (index == MAX_BUF)
break; /* buffer is full - no more chars can be written */
5. when you read the char - check isupper - and put it in the array only if this is true