How to convert a string to a integer in C


First extract characters from left bracket '(' using strchr() function.

char *name="The Matrix(1999)";
char *ps;
ps=strchr(name,'(');

Then add each character within brackets () to an char array

char y[5]=""; int  p;
for (p=1;p<strlen(ps+1);p++) {
   y[p-1]=ps[p];
}
y[4]='\0';

Lastly convert resultant string to integer using atoi() function

year=atoi(y);
printf("year=%d",year);

You can now apply required filters ti create array of strings of all movies prior to 2008

Updated on: 27-Jan-2020

409 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements