-1

I know I essentially need to replace the "\n" with a "\0", but how would I access the array to incorporate this? I can not use string.h library or any other libraries.

#include <stdio.h>
#include <stdlib.h>


int main() {

    char buffer[32];
    char digits[3];

    printf("Enter name:");
    fgets(buffer,32,stdin);



    printf("Enter age:");
    fgets(digits,3,stdin);

    char *name;
    name = buffer;

    int *age;
    age = atoi(digits);

    happyBirthday(name,age);

    return 0;
}

AMCode96
  • 288
  • 1
  • 13

1 Answers1

2
int i = 0;
while (buffer[i] != '\0')
{
    if (buffer[i] == '\n') buffer[i] = '\0';
    ++i;
}
Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • 2
    Or more succinctly using a for loop: `for (int i = 0; buffer[i] != '\0'; i++) { if (buffer[i] == '\n') { buffer[i] = '\0'; } }` We could even use pointer arithmetic to take out the unnecessary `i` variable. Though `i`, if not locally scoped to the loop, could be useful for keeping track of the length of the resulting string. – Chris Sep 08 '21 at 20:37