I'm doing a program that needs you to find half a string vector, but that half can not cut any words, so that half would have to be the next space found after half. Soon after finding this half the program must divide the vector into 2 string vectors. One with the first part and another with the second.
So far what I have is this here:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void DivideString(char *Texto, char *Metade1, char *Metade2, int tamanho,int
metade){
int i;
for(i = 0; i < metade; i++){
Metade1[i] = Texto[i];
}
for(i=metade; i<tamanho; i++){
Metade2[i] = Texto[i];
}
}
int CalculaMeio(int metade, char *Texto){
while(Texto[metade]!=' '){
metade++;
}
return metade;
}
int main(){
char *Texto, *Metade1, *Metade2;
int i;
Texto = (char*)malloc(1000*sizeof(char));
Texto = "Testando como funcionam strings em c para trabalho";
int tamanho = strlen(Texto), metade = tamanho/2;
printf("tamanho: %d metade: %d\n",tamanho, metade);
metade = CalculaMeio(metade, Texto);
printf("nova metade: %d\n",metade );
Metade1=(char*)malloc(metade*sizeof(char));
Metade2=(char*)malloc((tamanho-metade)*sizeof(char));
DivideString(Texto, Metade1, Metade2, tamanho, metade);
printf("%s\n",Metade1 );
printf("%s\n",Metade2 );
return 0;
}
Running this program has the following:
As you can see from the image, the program prints the first vector correctly but the second vector is not printed.
Would this be a problem with the buffer?