How to delete a newline within a string - c

I want to delete a Newline '\n' within a string.
char *string ="hallo\n";
int i=0;
int length = sizeof(string);
while(i<length)
{
if(string[i+1] == '\n')
{
string[i+1] = '\0';
break;
}
i++;
}
printf("%s",string);
printf("world");
I know that I could just spawn a new array and it works like this
char *string ="hallo\n";
int i=0;
int length = sizeof(string);
int lengthNew = length -1;
char newStr[lengthNew];
while(i<length)
{
printf("Char ist %c:",string[i]);
newStr[i] = string[i];
if(string[i+1] == '\n')
break;
i++;
}
But why using stack if I just could substitude one character in the old array?

Based on your comment, I offer a completely different, yet better solution: strftime:
time_t clock = time(NULL);
char buf[1024];
strftime(buf, sizeof buf, "%c", localtime(&clock);
printf("The date is: %s\n", buf);
The %c format is the same as is used by ctime, but strftime is more flexible.

If the newline is always the last character of the string, you could code it like you have described.
Otherwise you'd have to create a second character buffer and copy the characters to the second buffer. The reason for this is that in C the \0 character marks the end of the string.
If you have a string like this: "this \n is \n a \n test", then after your replacement the memory would look like this: "this \0 is \0 a \0 test". Most C programs will simply interpret this as the string "this " and ignore everything after the first null.
EDIT
As other have pointed out, there are also other problems with your code. sizeof() will return the size of the character pointer, not the length of the string. It is also not possible to modify a readonly string literal.

char *string = ctime(&myTimeT);
char *c = strrchr(string, '\n');
if (c != NULL)
*(c) = '\0';

Related

Why copying a character to a char array results in weird behaviour

I have the following code which aims to implemente a simple LZW based text compression:
int compress(FILE text_file, FILE *output) {
char input[20];
short code = 0;
short size = 0;
memset(input, '\0', 20);
char c = fgetc(text_file);
while (c != EOF) {
strncat(input, &c, 1);
code = getCodeFromInput(input);
if ((size + 1) > 1 && code == -1) {
addInput(input);
fwrite(&code, 2, 1, output);
strcpy(input, &c);
printf(%s, input);
size = 1;
code = 0;
} else {
size++;
}
c = fgetc(text_file);
}
fwrite(&input, 2, 1, output);
return 0;
}
So after finding a suitable string I need to override my string input with with the last char I've received. When using strcpy() I can easily copy arrays to my input string, but when trying to copy a single character, if I print the result I will get the expected output plus some weird character, for example copying the caracter 'a' I get a�. Is this expected? Why does strcpy() behave like that?
The standard defines strcpy as a function copying a NUL terminated string, you are copying the last input (a character), but even if you get his address, it is not a NUL terminated string:
strcpy(input, &c);
you can try using a compound literal:
strcpy(input, (char[2]){c, '\0'}); // A NUL terminated string
Also, notice that fgetc wants an int instead of a char in order to handle EOF:
char c = fgetc(text_file);
should be
int c = fgetc(text_file);
in consequence:
strcpy(input, (char[2]){(char)c, '\0'}); // A NUL terminated string

puts() output is appended "time" string

I get very unexpected output from quite simple code
char ch = getchar(), word[100], *p = word;
while (ch != '\n') {
*(p++) = ch;
ch = getchar();
}
puts(word);
output of any 17 character input is appended by "time" like
12345678901234567time
if exceeds "time" is overwritten like
1234567890123456789me
Am I doing something wrong?
puts expects a pointer to string. And a string needs to have a terminating null character - \0 - to signify where the string ends.
But in your case, you did not write the \0 at the end to signify that the string ends there.
You need to do:
char ch = getchar(), word[100], *p = word;
/* Also check that you are not writing more than 100 chars */
int i = 1;
while(ch != '\n' && i++ < 100){
*(p++) = ch;
ch = getchar();
}
*p = '\0'; /* write the terminaring null character */
puts(word);
Before, when you were not writing the terminating null character you could not expect anything determinate to print. It could also have been 12345678901234567AnyOtherWord or something.
There are multiple issues in your code:
You do not null terminate the string you pass to puts(), invoking undefined behavior... in your case, whatever characters happen to be present in word after the last one read from stdin are printed after these and until (hopefully) a '\0' byte is finally found in memory.
You read a byte from stdin into a char variable: this does not allow you to check for EOF, and indeed you do not.
If you read a long line, you will write bytes beyond the end if the word array, invoking undefined behavior. If the end of file is encountered before a '\n' is read from stdin, you will definitely write beyond the end of the buffer... Try for example giving an empty file as input for your program.
Here is a corrected version:
char word[100];
char *p = word;
int ch;
while ((ch = getchar()) != EOF && ch != '\n') {
/* check for long line: in this case, we truncate the line */
if (p < word + sizeof(word) - 1) {
*p++ = ch;
}
}
*p = '\0';
puts(word);

Printing a string due to a new line

Is there any efficient (- in terms of performance) way for printing some arbitrary string, but only until the first new line character in it (excluding the new line character) ?
Example:
char *string = "Hello\nWorld\n";
printf(foo(string + 6));
Output:
World
If you are concerned about performance this might help (untested code):
void MyPrint(const char *str)
{
int len = strlen(str) + 1;
char *temp = alloca(len);
int i;
for (i = 0; i < len; i++)
{
char ch = str[i];
if (ch == '\n')
break;
temp[i] = ch;
}
temp[i] = 0;
puts(temp);
}
strlen is fast, alloca is fast, copying the string up to the first \n is fast, puts is faster than printf but is is most likely far slower than all three operations mentioned before together.
size_t writetodelim(char const *in, int delim)
{
char *end = strchr(in, delim);
if (!end)
return 0;
return fwrite(in, 1, end - in, stdout);
}
This can be generalized somewhat (pass the FILE* to the function), but it's already flexible enough to terminate the output on any chosen delimiter, including '\n'.
Warning: Do not use printf without format specifier to print a variable string (or from a variable pointer). Use puts instead or "%s", string.
C strings are terminated by '\0' (NUL), not by newline. So, the functions print until the NUL terminator.
You can, however, use your own loop with putchar. If that is any performance penalty is to be tested. Normally printf does much the same in the library and might be even slower, as it has to care for more additional constraints, so your own loop might very well be even faster.
for ( char *sp = string + 6 ; *sp != '\0'; sp++ ) {
if ( *sp == '\n' ) break; // newline will not be printed
putchar(*sp);
}
(Move the if-line to the end of the loop if you want newline to be printed.)
An alternative would be to limit the length of the string to print, but that would require finding the next newline before calling printf.
I don't know if it is fast enough, but there is a way to build a string containing the source string up to a new line character only involving one standard function.
char *string = "Hello\nWorld\nI love C"; // Example of your string
static char newstr [256]; // String large enough to contain the result string, fulled with \0s or NULL-terimated
sscanf(string + 6, "%s", newstr); // sscanf will ignore whitespaces
sprintf(newstr); // printing the string
I guess there is no more efficient way than simply looping over your string until you find the first \n in it. As Olaf mentioned it, a string in C ends with a terminating \0 so if you want to use printf to print the string you need to make sure it contains the terminating \0 or yu could use putchar to print the string character by character.
If you want to provide a function creating a string up to the first found new line you could do something like that:
#include <stdio.h>
#include <string.h>
#define MAX 256
void foo(const char* string, char *ret)
{
int len = (strlen(string) < MAX) ? (int) strlen(string) : MAX;
int i = 0;
for (i = 0; i < len - 1; i++)
{
if (string[i] == '\n') break;
ret[i] = string[i];
}
ret[i + 1] = '\0';
}
int main()
{
const char* string = "Hello\nWorld\n";
char ret[MAX];
foo(string, ret);
printf("%s\n", ret);
foo(string+6, ret);
printf("%s\n", ret);
}
This will print
Hello
World
Another fast way (if the new line character is truly unwanted)
Simply:
*strchr(string, '\n') = '\0';

Removing last character in C

The program I am writing needs to remove an ampersand character if it is the last character of a string. For instance, if char* str contains "firefox&", then I need to remove the ampersand so that str contains "firefox". Does anyone know how to do this?
Just set the last char to be '\0':
str[strlen(str)-1] = '\0';
In C, \0 indicates a string ending.
Every string in C ends with '\0'. So you need do this:
int size = strlen(my_str); //Total size of string
my_str[size-1] = '\0';
This way, you remove the last char.
To be on the safe side:
if (str != NULL)
{
const unsigned int length = strlen(str);
if ((length > 0) && (str[length-1] == '&')) str[length-1] = '\0';
}
Just for reference, the standard function strchr does just that. It effectively splits the string on a given set of characters. It works by substituting a character with 0x00
Example shamelessly stolen from: cplusplus.com
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "This is a firefox& string";
char * pch;
printf ("Looking for the '&' character in \"%s\"...\n",str);
pch=strchr(str,'&');
while (pch!=NULL)
{
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'&');
}
return 0;
}
Check the last character and if it is '&', replace it with '\0' (null terminator):
int size = strlen(my_str);
if (str[size - 1] == '&')
str[size - 1] = '\0';

c getline() - how do I parse a line?

I'm at a loss as far as how to parse the line in getline(). I would like look at each char that is in the line.
So if someone were to type in: "Hello" into stdin, I'd like to be able to able to access the char array in this manner:
line[0] = 'H'
line[1] = 'e'
line[2] = 'l'
line[3] = 'l'
line[4] = 'o'
line[5] = '/0';
I've looked at getchar(), but I want to try and use getline() as I feel like it is more convenient. I've also looked at scanf(), but it skips whitespaces and doesn't let me parse the input as nicely as getchar() or getline().
Here is simple code that attempts at getting the first char of a line via stdin, but results in a seg fault:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int len;
int nbytes = 100;
char *line = (char *) malloc (nbytes + 1);
while(getline(&line, &nbytes, stdin) > 0){
printf("first char: %s", line[0]); //try and get the first char from input
/**
* other code that would traverse line, and look at other chars
*/
};
return 0;
}
Thanks.
Use the %c format specifier to print out a single character.
The code
printf("first char: %s", line[0])
will try to treat line[0] as the address of a char array. If you just want to print out the first character, change it to
printf("first char: %c", line[0])
// ^
There are a couple of other small changes you could consider in other parts of your code:
you don't need to cast the return from malloc
you should free(line) after your while loop

Resources