sorry, simple mistakes, fixed now
From lesson 9 C strings (cprogrmming.com)
This program builds with no errors, but 2 warnings.
But the problem is when I run it, after I enter the first input (name) I get
"Program received signal: “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb)"
Code:
#include <stdio.h>
#include <string.h>
//this fuction removes newline from string entered by fgets
void strip_newline(char *str, int size){
int i;
for (i = 0; i < size; ++i) {
if (str[i] == '\n') {
str[i] = '\0';
return; // we're donr so exit the function by returning
}
}
//if we get this there must be no newline
}
int main()
{
char name[50];
char lastname[50];
char fullname[100]; // big enough to hold name + last name
printf("Please enter your first name: ");
fgets(name, 50, stdin);
strip_newline(name, 50);
//strcmp returns 0 when 2 names are equal
if (strcmp (name, 'Alex') == 0 ) {
printf("That's my name too!\n");
}
else {
printf("That's not my name.\n");
}
//find the length of your name
printf("Your name is %d letters long", strlen, name);
printf("Enter last name");
fgets(lastname, 50, stdin);
strip_newline(lastname, 50);
fullname[0] = '\0'; //strcat will look for 0 and add second string
//starting from that location
strcat(fullname, name); //copy name to full name
strcat(fullname, " "); //add space between names
strcat(fullname, lastname);
printf("Your full name is %s\n", fullname);
getchar();
return 0;
}