You are on page 1of 3

// C program to illustrate

// strcmp() function
#include <stdio.h>
#include <string.h>
  
int main()
{
  
    char leftStr[] = "g f g";
    char rightStr[] = "g f g";
  
    // Using strcmp()
    int res = strcmp(leftStr, rightStr);
  
    if (res == 0)
        printf("Strings are equal");
    else
        printf("Strings are unequal");
  
    printf("\nValue returned by strcmp() is:  %d", res);
    return 0;
}
Output:
Strings are equal
Value returned by strcmp() is: 0
Program 1:
filter_none
edit
play_arrow
brightness_4
// C code to demonstrate the working of
// strrchr()
  
#include <stdio.h>
#include <string.h>
  
// Driver function
int main()
{
  
    // initializing variables
    char st[] = "GeeksforGeeks";
    char ch = 'e';
    char* val;
  
    // Use of strrchr()
    // returns "ks"
    val = strrchr(st, ch);
  
    printf("String after last %c is : %s \n",
           ch, val);
  
    char ch2 = 'm';
  
    // Use of strrchr()
    // returns null
    // test for null
    val = strrchr(st, ch2);
  
    printf("String after last %c is : %s ",
           ch2, val);
  
    return (0);
}
Output:
String after last e is : eks
String after last m is : (null)

Converting a String to a Number


In C programming, we can convert a string of numeric characters to
a numeric value to prevent a run-time error. The stdio.h library
contains the following functions for converting a string to a number:

 int atoi(str) Stands for ASCII to integer; it converts str to the


equivalent int value. 0 is returned if the first character is not a
number or no numbers are encountered.
 double atof(str) Stands for ASCII to float, it converts str to the
equivalent double value. 0.0 is returned if the first character is
not a number or no numbers are encountered.
 long int atol(str) Stands for ASCII to long int, Converts str to the
equivalent long integer value. 0 is returned if the first
character is not a number or no numbers are encountered.

The following program demonstrates atoi() function:

#include <stdio.h>
int main()
{char *string_id[10];
int ID;
printf("Enter a number: ");
gets(string_id);
ID = atoi(string_id);
printf("you enter %d ",ID);
return 0;}

Output:

Enter a number: 221348


you enter 221348

You might also like