How would I write a function in C that only prints out the l

How would I write a function in C that only prints out the last 3 charcters in a string taken in from agv[1] on the command line (not prompting user input)? My letter range is from 3-40 too.

Solution

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

char *lastThree(int s, const char *input);
//function to extract last n digits of the string
char *lastThree(int s, const char *input)
{
char *ret = input; //initialize one new pointer
   int i;
  
   for (i = 0; i <= s; i++)
   {
       ret[i] = input[strlen(input) - (s - i)]; // getting the digits
   }
  
   return ret;
}

int main( int argc, char *argv[] ) {

  
int j = 0;
char s[40];
char *lastthreeletter;
int k=0;
while(argv[1][j] != \'\\0\'){ // loop to get the args
s[k]=argv[1][j++];
k++;
}
if(k>=3 && k<=40){ // condition to check the range of input
lastthreeletter=lastThree(3,s);
for(k=0;k<3;k++){
printf(\"%c\\t\",lastthreeletter[k]); //printing the last three characters
}
}else{
printf(\"Argument should be between range of 3-40 \ \");
}
return 0;
}

-------------------------output--------------------------------------------------------
sh-4.3$ main ja
Argument should be between range of 3-40   
sh-4.3$ main america   
i c a sh-4.3$

How would I write a function in C that only prints out the last 3 charcters in a string taken in from agv[1] on the command line (not prompting user input)? My

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site