Contexte:Mystère de la mystérieuse P
Je suis en train de créer un programme qui prend un nom d'utilisateur (en supposant que l'entrée est propre), et imprime les initiales du nom.
Objectif:
- Essayer ma main à la programmation C avec CS50
- moi-même Se familiariser avec malloc & realloc
code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
string prompt(void);
char *getInitials(string input);
char *appendArray(char *output,char c,int count);
//Tracks # of initials
int counter = 0;
int main(void){
string input = prompt();
char *output = getInitials(input);
for(int i = 0; i < counter ; i++){
printf("%c",toupper(output[i]));
}
}
string prompt(void){
string input;
do{
printf("Please enter your name: ");
input = get_string();
}while(input == NULL);
return input;
}
char *getInitials(string input){
bool initials = true;
char *output;
output = malloc(sizeof(char) * counter);
for(int i = 0, n = strlen(input); i < n ; i++){
//32 -> ASCII code for spacebar
//9 -> ASCII code for tab
if(input[i] == 32 || input[i] == 9){
//Next char after spaces/tab will be initial
initials = true;
}else{//Not space/tab
if(initials == true){
counter++;
output = appendArray(output,input[i],counter);
initials = false;
}
}
// eprintf("Input[i] is : %c\n",input[i]);
// eprintf("Counter is : %i\n",counter);
// eprintf("i is : %i\n",i);
// eprintf("n is : %i\n",n);
}
return output;
}
char *appendArray(char *output,char c,int count){
// allocate an array of some initial (fairly small) size;
// read into this array, keeping track of how many elements you've read;
// once the array is full, reallocate it, doubling the size and preserving (i.e. copying) the contents;
// repeat until done.
//pointer to memory
char *data = malloc(0);
//Increase array size by 1
data = realloc(output,sizeof(char) * count);
//append the latest initial
strcat(data,&c);
printf("Value of c is :%c\n",c);
printf("Value of &c is :%s\n",&c);
for(int i = 0; i< count ; i++){
printf("Output: %c\n",data[i]);
}
return data;
}
Problème:
La sortie n'est pas ce que je m'attendais car il y a un mystérieux P apparaissant dans la sortie.
Par exemple, quand je rentre le nom de Barack Obama, au lieu d'obtenir le résultat: BO, je reçois le résultat BP et la même chose se produit pour quelque nom que je choisis d'entrer, avec la dernière initiale étant toujours P.
sortie:
Please enter your name: Barack Obama
Value of c is :B
Value of &c is :BP
Output: B
Value of c is :O
Value of &c is :OP
Output: B
Output: P
BP
ce que je l'ai fait:
J'ai tracé le problème à la fonction appendArray, et plus précisément à la valeur de & c (adresse de c) si je ne sais pas ce qui est faisant apparaître le P, ce que cela signifie, pourquoi il apparaît et comment je peux m'en débarrasser.
La valeur de P s'affiche peu importe quand je l'entrée.
Un aperçu de ce qui se passe et ce que je peux faire pour le résoudre sera très apprécié.
Merci!
Il y a un [CS50 site d'échange de pile] (http://cs50.stackexchange.com/) dans le cas où vous êtes intéressé . – pmg
@pmg Je ne le savais pas. Merci de l'avoir signalé! –
'output = malloc (compteur sizeof (char) *);' -> 'output = malloc (++ compteur); * output = 0; ' – BLUEPIXY