Home > OS >  Print first N characters of a string (C)
Print first N characters of a string (C)

Time:10-24

My objective is to print out the first N characters (the len variable) of a string using a simple for loop. However, the code doesn't seem to work. e.g. with the input printFrstChars("Example",3) the code should print out Exa, but it does not print out anything.

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

void printFrstChars (char inp[], int len)
{
    for(int i = 0; inp[i] != '\0' && i <= len; i  ){
        printf("%s", inp[i]);

    }

}

int main ()
{   
    int len = 0;
    char inp[100];
    printf("Input string and length:\n");
    scanf("           
  • Related