How do I reduce the size of a string in C? In my program it is implemented as follows:
char nomeString[] = "nomedoarquivo.txt";
I want to cut the ".txt" from the end of the string .
How do I reduce the size of a string in C? In my program it is implemented as follows:
char nomeString[] = "nomedoarquivo.txt";
I want to cut the ".txt" from the end of the string .
As strings in C end with a null, just place a null just after the text that should stay. As if knowing that what should disappear are the last 4 characters just put the terminator in the string size minus 4. Thus:
#include <stdio.h>
#include <string.h>
int main(void) {
char nomeString[] = "nomedoarquivo.txt";
nomeString[strlen(nomeString) - 4] = '#include <stdio.h>
#include <string.h>
int main(void) {
char nomeString[] = "nomedoarquivo.txt";
nomeString[strlen(nomeString) - 4] = '%pre%';
printf("%s", nomeString);
}
';
printf("%s", nomeString);
}
See running on ideone and on CodingGround .