Carl
Carl

Reputation: 101

How to make my c-program recognize "\n" characters and ignore them

I am making a program that is suppose to check that a certain input-string only contains a certain type of characters (ASCII-characters). I have a string of characters that belong to the ASCII-standard, and for each of the characters in my input-string, I check (character by character) if it exist in the ASCII-string. If the character do exist, I will move them to a new string, an output string. If they do not exist, I inform the user and exit the program.

However, the input-string I am making could both be created from the command line, but also from a text-file. My textfiles always seem to contain several new-lines, and they always end with a new-line ("\n"-character) which always makes my program to report errors, either in the end of the string or in the middle.

How do I rearrange the code of the below program in order for it to recognize the "\n"-characters? Below is a simplification of the code, not showing how I define the inputstring nor the string with the ASCII-characters.

char inputstring[];
char outputstring[];
char asciicharacters[];
* Here I read a text-file to the inputstring, and the ASCII-characters to the asciicharacters-string *

int k,i=0;

for(i=0;i<strlen(inputstring);i++)
{

    for(k=0;k<strlen(asciicharacters);k++)
    {

        if(inputstring[i] == asciicharacters[k])
        {
            outputstring[i] = inputstring[i];
            break;
        }
        else if(k == strlen(asciicharacters)-1)
        {
            printf("The character: %c, is not ASCII-standard",input[i]);
            exit(1);
        }

    }

}

How do I create another condition that makes the program skip (and accept as an ASCII-standard) when I reach a "\n"-character?

Upvotes: 1

Views: 783

Answers (1)

jdigital
jdigital

Reputation: 12266

You can add this else if before the final else if.

else if( inputstring[i] == '\n' )
{
    break;
}

Upvotes: 2

Related Questions