Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

String Functions (C)

The document discusses various string handling and conversion functions in C including: - strlen(), strcpy(), strcat(), strcmp(), strstr() for manipulating and comparing strings - atoi(), strtol(), sscanf() for converting strings to integers - fgets() for reading input from stdin or files, taking a character array, maximum characters, and file pointer - stdin refers to standard input typically the keyboard

Uploaded by

Ibrahim Labib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

String Functions (C)

The document discusses various string handling and conversion functions in C including: - strlen(), strcpy(), strcat(), strcmp(), strstr() for manipulating and comparing strings - atoi(), strtol(), sscanf() for converting strings to integers - fgets() for reading input from stdin or files, taking a character array, maximum characters, and file pointer - stdin refers to standard input typically the keyboard

Uploaded by

Ibrahim Labib
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

String Functions (C):

● strlen(): This function returns the length of a given string. For


example:

#include <string.h>

char str[] = "Hello, World!";


int len = strlen(str);
printf("The length of the string is %d.\n", len);

● strcpy(): This function copies a string from one location to another.


For example:

#include <string.h>

char str1[] = "Hello, World!";


char str2[20];
strcpy(str2, str1);
printf("str2: %s\n", str2);

● strcat(): This function concatenates (appends) one string to the end of


another. For example:

#include <string.h>

char str1[20] = "Hello, ";


char str2[] = "World!";
strcat(str1, str2);
printf("str1: %s\n", str1);

● 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>

char str1[] = "Apple";


char str2[] = "Banana";
int result = strcmp(str1, str2);
if (result < 0) {
printf("%s is lexicographically smaller than %s.\n", str1, str2);
} else if (result > 0) {
printf("%s is lexicographically larger than %s.\n", str1, str2);
} else {
printf("%s is identical to %s.\n", str1, str2);
}

● strstr(): This function returns a pointer to the first occurrence of a


substring within a string. For example:

#include <string.h>

char str1[] = "Hello, World!";


char str2[] = "World";
char *result = strstr(str1, str2);
if (result != NULL) {
printf("'%s' found in '%s' at position %ld.\n", str2, str1, result - str1);
} else {
printf("'%s' not found in '%s'.\n", str2, str1);
}

● memcpy(): This function copies a block of memory from one location


to another. It can be used to copy strings as well as other types of
data. For example:

#include <string.h>

char str1[] = "Hello, World!";


char str2[20];
memcpy(str2, str1, sizeof(str1));
printf("str2: %s\n", str2);

● memset(): This function fills a block of memory with a specific value. It


can be used to set all characters of a string to a specific value. For
example:

#include <string.h>
char str[20] = "Hello, World!";
memset(str, '*', 7);
printf("str: %s\n", str);

● sprintf(): This function writes a formatted string to a character array. It


can be used to create a string from multiple variables. For example:

#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);

● sscanf(): This function reads a formatted string from a character array.


It can be used to extract values from a string and store them in
variables. For example:

#include <stdio.h>

char str[] = "x = 5, y = 3.14";


int x;
double y;
sscanf(str, "x = %d, y = %lf", &x, &y);
printf("x = %d, y = %.2f\n", x, y);

● strtok() : This function tokenize a string, it used to split a string into a


sequence of tokens. For example:

#include <string.h>

char str[] = "Hello, World! How are you?";


char *token = strtok(str, " ");
while (token != NULL) {
printf("%s\n", token);
token = strtok(NULL, " ");
}

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.

String or Char to Int:

In C, you can convert a string or character representing a number into an


integer using the atoi() function, which is a part of the standard C library. The
atoi() function takes a string as its argument and returns the corresponding
integer value. Here's an example of how to use atoi():

#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:

Yes, there are other ways to convert a string or character representing a


number into an integer in C. One common alternative is to use the strtol()
function (short for "string to long"). This function is also a part of the standard
C library and is similar to atoi(), but it also allows you to specify the base of the
number being converted (for example, base 10 for decimal numbers or base 16
for hexadecimal numbers). Here's an example of how to use strtol():
#include <stdio.h>
#include <stdlib.h>

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 and Stdin:

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.

You might also like