Thread: Remove ASCII and Newline Characters

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    2

    Post Remove ASCII and Newline Characters

    I have some simple code. Within the code I have two strings which I use strcat() to combine. In my output I have two ASCII special characters appearing that I would like to get rid of. Does anyone know how to get rid of these characters?

    I would also like to get rid of the newline character between the strings.

    Here is the code if you would like to take a look:
    Code:
     
     */
    
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
    	char string[10], string2[15], string3[25];
    	int(age);
    
    	printf("Please enter your first name.\n");
    	fgets(string, 10, stdin);
    
    	printf("Please enter your last name. \n");
    	fgets(string2, 15, stdin);
    
    	printf("How old are you? \n");
    	scanf("%d", &age);
    
    	strcat(string3, string);
    	strcat(string3, " ");
    	strcat(string3, string2);
    
    	printf( "Your name is %s. \n", string3);
    
    	printf( "You are %d years old.", age);
    
    	getchar();
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Start with strcpy rather than strcat.

    Or initialise the destination string to be empty to start with.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    2
    Thank you Salem. Starting with strcpy() worked wonders for getting rid of the ASCII characters. Any recommendations for getting rid of the newline character?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    1) 'string3' is uninitialized, so the first character may not be '\0', which will cause strcat to work improperly.
    2) 'fgets' may append a newline. You need to search for it and strip it out, if present.
    3) 'strcat', along with any other function that doesn't take the size of the buffer into consideration, is unsafe. Unfortunately, strncat doesn't guarantee a null termination, so it is unsafe as well!
    4) Don't use 'magic constants'. Define a standard buffer size, or pass variables.

    Suggestion:

    Code:
    
    
    #include<stdio.h>
    #include<string.h>
    
    /*
    	Reads a line from standard input, and discards the newline, if present
    */
    char* getline( char* buf, size_t max )
    {
    	char
    		* nln,
    		* ret = fgets( buf, max, stdin );
    	if( ret )
    	{
    		nln = strchr( ret, '\n' );
    		if( nln )
    			*nln = '\0';
    	}
    	return ret;
    }
    
    /*
    	A safe strncat alternative
    */
    char* concat( char* buf, const char* end, size_t max )
    {
    	size_t
    		len = strlen( buf );
    	char
    		ch, 
    		* ptr = buf + len;
    	for( ;; )
    	{
    		if( ++len >= max )
    			break;
    		ch = *end++;	
    		if( ch == '\0' )
    			break;
    		*ptr++ = ch;	
    	}
    	*ptr = 0;
    	return buf;
    }
    
    /*
    	Sets the first character in a buffer to '\0'
    */
    char* reset( char* buf )
    {
    	*buf = '\0';
    	return buf;
    }
    
    #define MAX_BUF 64
    
    int main()
    {
    	char string[MAX_BUF], string2[MAX_BUF], string3[MAX_BUF];
    
    	printf("Please enter your first name.\n");
    	getline(string, sizeof( string ));
    
    	printf("Please enter your last name. \n");
    	getline(string2, MAX_BUF);
    
    	reset( string3 );
    	
    	concat(string3, string, MAX_BUF);
    	concat(string3, " ", MAX_BUF);
    	concat(string3, string2, MAX_BUF);
    
    	printf( "Your name is %s. \n", string3);
    
    	getchar();
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. ascii backspace, K&R book exercises
    By jjohhn in forum C Programming
    Replies: 10
    Last Post: 12-29-2005, 08:33 AM
  2. Inserting hex characters in the output
    By earth_angel in forum C++ Programming
    Replies: 5
    Last Post: 06-29-2005, 11:52 AM

Tags for this Thread