Reduce a string in C language

5

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 .

    
asked by anonymous 27.11.2016 / 17:42

1 answer

4

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 .

    
27.11.2016 / 17:53