String Functions (C)
String Functions (C)
#include <string.h>
#include <string.h>
#include <string.h>
● strcmp(): This function compares two strings and returns 0 if they are
identical, a negative value if the first string is lexicographically smaller
than the second, or a positive value if the first string is
lexicographically larger than the second. For example:
#include <string.h>
#include <string.h>
#include <string.h>
#include <string.h>
char str[20] = "Hello, World!";
memset(str, '*', 7);
printf("str: %s\n", str);
#include <stdio.h>
int x = 5;
double y = 3.14;
char str[20];
sprintf(str, "x = %d, y = %.2f", x, y);
printf("str: %s\n", str);
#include <stdio.h>
#include <string.h>
Please note that these functions also have different variations and options, it is
always a good idea to check the documentation of the C library you are using to
learn more about the function and its specific use cases.
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "123";
int num = atoi(str);
printf("The integer value of the string '%s' is: %d\n", str, num);
return 0;
}
In this example, the string "123" is passed to the atoi() function, which converts
it to the integer value 123 and assigns it to the variable num. The printf()
function is then used to print the value of num to the screen.
You can also convert a character to a integer by subtracting '0' from the
character value. For example:
char ch = '5';
int num = ch - '0';
Please note that, atoi() only converts the string to a valid int if the string only
contain digits, otherwise it will return 0.
Please let me know if you have any other question.
Another Way:
int main() {
char str[] = "123";
int num = (int) strtol(str, NULL, 10);
printf("The integer value of the string '%s' is: %d\n", str, num);
return 0;
}
In this example, the string "123" is passed as the first argument to the strtol()
function. The second argument is set to NULL, indicating that the entire string
should be used for the conversion. The third argument is set to 10, indicating
that the number is in base 10. This returns the same result as atoi().
Another way is by using sscanf() function. Here is an example :
#include <stdio.h>
int main() {
char str[] = "123";
int num;
sscanf(str, "%d", &num);
printf("The integer value of the string '%s' is: %d\n", str, num);
return 0;
}
In this example, the string "123" is passed as the first argument to the sscanf()
function. The second argument is a format string that specifies that the input
should be interpreted as an integer (%d). The third argument is the address of
an int variable where the converted value should be stored.
You can also use sscanf() function to convert a character to an int as well.
fgets() is a function in C that is used to read a line of text from a file or other
input stream. It takes three arguments: a pointer to a character array where the
input will be stored, the maximum number of characters to read, and a file
pointer that specifies the input stream.
Here's an example of how to use fgets() to read a line of text from the standard
input (the keyboard):
#include <stdio.h>
int main() {
char input[100];
printf("Enter a line of text: ");
fgets(input, sizeof(input), stdin);
printf("You entered: %s\n", input);
return 0;
}
In this example, the fgets() function is used to read a line of text from the
standard input (stdin) and store it in the input character array. The maximum
number of characters to read is specified as sizeof(input) (100 in this case).
The printf() function is then used to print the contents of the input array to the
screen.
stdin is a predefined file pointer in C that refers to the standard input, which is
typically the keyboard. It is typically passed as the third argument to functions
like fgets() and scanf() that read input from the user.
You can also use fgets() to read a line from a file, by passing the file pointer as
the third argument. Here's an example that reads a line from a file named
"data.txt":
#include <stdio.h>
int main() {
char input[100];
FILE *fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
fgets(input, sizeof(input), fp);
printf("The first line of the file is: %s\n", input);
fclose(fp);
return 0;
}
In this example, the fopen() function is used to open the "data.txt" file for
reading, and the returned file pointer is stored in the fp variable. The fgets()
function is then used to read a line from the file and store it in the input
character array. The fclose() function is used to close the file when we're done.
It's worth noting that fgets() also add newline character at the end of the string,
and also it reads a line including spaces until the newline character.
Please let me know if you have any other questions.