treat filled strings character to character in c

3

I'm used to other languages. Now I'm in C. I have filled in a "string" character to character and when printing it, nothing appears, I do not know if it is because of the null terminator or for what reason. I would appreciate help. I just want to delete the path from a complete path and I want to keep the file and the extension of the file:

#include <stdio.h>
char linea[50]="c:\pepe\resul.exe";
char nombrefich[50]="";
int x,cantidad,i;

void main()
{
    printf("Cadena inicial: %s\n",linea);
    x=strlen(linea);
    cantidad=1;
    do {
        x--;
        cantidad++;
    } while (linea[x]!='\');

    x++;
    i=1;
    for(x=x;x<strlen(linea);x++)
    {
        //printf("%c",linea[x]);
        nombrefich[i]=linea[x];
        i++;
    }
    nombrefich[i]='
#include <stdio.h>
char linea[50]="c:\pepe\resul.exe";
char nombrefich[50]="";
int x,cantidad,i;

void main()
{
    printf("Cadena inicial: %s\n",linea);
    x=strlen(linea);
    cantidad=1;
    do {
        x--;
        cantidad++;
    } while (linea[x]!='\');

    x++;
    i=1;
    for(x=x;x<strlen(linea);x++)
    {
        //printf("%c",linea[x]);
        nombrefich[i]=linea[x];
        i++;
    }
    nombrefich[i]='%pre%';
    printf("Resultado final: %s\n",nombrefich);
}
'; printf("Resultado final: %s\n",nombrefich); }
    
asked by Rodrigo 12.02.2018 в 17:36
source

1 answer

2

I had already replied in a comment, but apparently it has gone unnoticed, so I repeat it here.

The code of the question is basically correct (although it could be done in other ways), except for the detail that seems to assume that the index of the first character of a string is 1, for example in lines such as:

    cantidad=1;

or

    i=1;

when actually the arrays in C begin at 0 (which includes the strings, which do not stop being arrays of characters).

The reason why you did not get anything when printing, is that if you use 1 as the initial index you are copying the string one position beyond where it should be, and in its position 0 you have not written anything, so you leave it there what would have initially. And what is initially is a string terminator, since char nombrefich[50]=""; initializes the array with an empty string, that is, with a terminator in its first position.

That is to say. Before executing your loops, the nombrefich[] array contains:

Indice     -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| ...
              +---+---+---+---+---+---+---+---+---+---+---+---+---+---
Contenido  -> |
Indice     -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| ...
              +---+---+---+---+---+---+---+---+---+---+---+---+---+---
Contenido  -> |
    cantidad=1;
| r | e | s | u | l | . | e | x | e |
    i=1;
| ? | ? | ... +---+---+---+---+---+---+---+---+---+---+---+---+---+---
| ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ... +---+---+---+---+---+---+---+---+---+---+---+---+---+---

where ? represents any character that is unknown to us, and nombrefich[] is the string terminator. After executing your loops, the printf() array will contain:

Indice     -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| ...
              +---+---+---+---+---+---+---+---+---+---+---+---+---+---
Contenido  -> |
Indice     -> | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10| 11| 12| ...
              +---+---+---+---+---+---+---+---+---+---+---+---+---+---
Contenido  -> |%pre% | r | e | s | u | l | . | e | x | e | %pre%| ? | ? | ...
              +---+---+---+---+---+---+---+---+---+---+---+---+---+---
| ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ? | ... +---+---+---+---+---+---+---+---+---+---+---+---+---+---

When you ask %s to print that string (with i ), since the first thing you find is the terminator cantidad , it stops there without printing anything else.

The solution, of course, is to initialize %code% with 0 instead of 1. The variable %code% is apparently not used at all, so it can be removed, as noted by @NaCl in a comment.

    
answered by 12.02.2018 в 19:09