How to get the last 4 characters of a string in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

int main(void) {
    char s[] = "java c c++ c# php python";
    char last[5];

    int N = 4;
    strncpy(last, s + strlen(s) - N, N); 
    last[N] = '\0';
      
    puts(last);
    
    return 0;
}
  
  
  
  
/*
run:
  
thon
  
*/

 



answered Oct 5, 2021 by avibootz

Related questions

1 answer 124 views
1 answer 255 views
1 answer 147 views
1 answer 154 views
1 answer 101 views
1 answer 219 views
...