When I am trying to compile my code, and make it, it is saying this " make initials
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow initials.c -lcrypt -lcs50 -lm -o initia"
Can you show the code and the commands you are using to compile it?
yes I can,
This is my code
/*
CS50 AP Period 6
Brian Franklin
PSET1 initals
7 September 2018
*/
//Initializing the libraries
# include <stdio.h>
# include <ctype.h>
# include <string.h>
//declare function
void getInitials(char* name );
int main(){
//declaring a string, assuming that the users name doesn't exceed 40 characters
char name[40];
//getting the users input (their full name)
fgets(name, sizeof(name), stdin);
//Print the initials
printf("");
getInitials(name);
return 0;
}
void getInitials(char* name ){
int i=0;
//If the string length is greater than 0 then there is at least one character at index 0
//and if that character is alpha numeric then it must be the first letter for the intials
if(strlen(name) > 0 && isalpha(name[0]))
printf("%c", toupper(name[0] ));
//while the current character in name does not equal the last character in the string 'name'
// go through each character of the string
while(name[i] != '\0'){
//if the character is a space, then the next character should be printed after that
if( isspace(name[i]) != 0){
//loop all of the spaces for human input error
while(isspace(name[i]) && i <= strlen(name)){i++;}
printf("%c", toupper(name[i+1] ));
}
i++; //change i by 1
}
}
IIRC if it shows just the long clang
command after running make
, then that means youâve successfully compiled your program. There should then be a file called initials
(the executable) alongside the initials.c
file.
Itâs been long from my c days, but iâll give it a shot. I think youâre going out of bounds when you did this i+1
. Also, isnât this supposed to be right before âiâ is incremented? Also, second while loop is by my account redundant. From her, you just need a increment.
Try this:
if( isspace(name[i])){
printf("%c", toupper(name[i] ));
i++;
}
And for just to be sure add some text in your array.
char name[14]={'F', 'r', 'e', 'e', ' ', 'C', 'o', 'd', 'e', 'C', 'a', 'm', 'p', '\0'};
Fifth element is empty space.
Yes, I was able to get your code to run just fine in C on my ubuntu system. The only thing I got was a warning for trying to printf
and empty string:
test.c:25:12: warning: zero-length gnu_printf format string [-Wformat-zero-length]
printf("");
But Iâm a big fan of debug statements. I added in some so I could watch your code:
void getInitials(char* name ){
int i=0;
//If the string length is greater than 0 then there is at least one character at index 0
//and if that character is alpha numeric then it must be the first letter for the intials
if(strlen(name) > 0 && isalpha(name[0])) {
printf("%c\n", toupper(name[0] ));
printf("string length = %lu\n", strlen(name));
printf("*** here 1 - %d\n", i);
//while the current character in name does not equal the last character in the string 'name'
// go through each character of the string
while(name[i] != '\0'){
printf("*** here 2 - %d\n", i);
//if the character is a space, then the next character should be printed after that
if( isspace(name[i]) != 0){
printf("*** here 3 - %d\n", i);
//loop all of the spaces for human input error
while(isspace(name[i]) && i <= strlen(name)){i++;}
printf("*** here 4 - %d\n", i);
printf("%c\n", toupper(name[i+1] ));
}
i++; //change i by 1
}
}
}
When I run it and enter the string âhow are youâ I get this:
how are you
H
string length = 12
*** here 1 - 0
*** here 2 - 0
*** here 2 - 1
*** here 2 - 2
*** here 2 - 3
*** here 3 - 3
*** here 4 - 4
R
*** here 2 - 5
*** here 2 - 6
*** here 2 - 7
*** here 3 - 7
*** here 4 - 8
O
*** here 2 - 9
*** here 2 - 10
*** here 2 - 11
*** here 3 - 11
*** here 4 - 12
Remember that that string is 0 indexed so when i is 12 and then you add one to it, you are actually looking for index 13, which doesnât exist.
You can also notice that while you get the first initial, the others are off by one.
caveat - I havenât done C in a really long time. I canât remember: when did Growing Pains go off the air?