The document provides an overview of arrays and strings in C programming, explaining how to declare and use arrays to store multiple values and perform operations like calculating averages. It also discusses string handling, highlighting the differences between character arrays and strings, and introduces functions for string manipulation and conversion from strings to numeric values. Additionally, it addresses limitations of the scanf function in reading strings and presents alternative methods like fgets for user input.
The document provides an overview of arrays and strings in C programming, explaining how to declare and use arrays to store multiple values and perform operations like calculating averages. It also discusses string handling, highlighting the differences between character arrays and strings, and introduces functions for string manipulation and conversion from strings to numeric values. Additionally, it addresses limitations of the scanf function in reading strings and presents alternative methods like fgets for user input.
integers, you can create an array for it. Here, we declared an array, mark, of floating-point type. And its size is 5. Meaning, it can hold 5 floating- point values. It's important to note that the size and type of an array cannot be changed once it is declared. // Program to take 5 values from the user and store them in an array // Print the elements stored in the array #include <stdio.h> int main() { int values[5]; printf("Enter 5 integers: "); // taking input and storing it in an array for(int i = 0; i < 5; ++i) { scanf("%d", &values[i]); } printf("Displaying integers: "); // printing elements of an array for(int i = 0; i < 5; ++i) { printf("%d\n", values[i]); } return 0; } // Program to find the average of n numbers using arrays #include <stdio.h> int main() { int marks[10], i, n, sum = 0; double average; printf("Enter number of elements: "); scanf("%d", &n); for(i=0; i < n; ++i) { printf("Enter number%d: ",i+1); scanf("%d", &marks[i]); // adding integers entered by the user to the sum variable sum += marks[i]; } // explicitly convert sum to double // then calculate average average = (double) sum / n; printf("Average = %.2lf", average); return 0; } A String in C is nothing but a collection of characters in a linear sequence. ‘C’ always treats a string a single data even though it contains whitespaces. A single character is defined using single quote representation. A string is represented using double quote marks. A C String is a simple array with char as a data type. ‘C’ language does not directly support string as a data type. Hence, to display a String in C, you need to make use of a character array. When writing interactive programs which ask the user for input, C provides the scanf(), gets(), and fgets() functions to find a line of text entered from the user.
When we use scanf() to read, we use the “%s” format
specifier without using the “&” to access the variable address because an array name acts as a pointer. The problem with the scanf function is that it never reads entire Strings in C. It will halt the reading process as soon as whitespace, form feed, vertical tab, newline or a carriage return occurs. Suppose we give input as “Guru99 Tutorials” then the scanf function will never read an entire string as a whitespace character occurs between the two names. The scanf function will only read Guru99. The fgets() arguments are :
the string name,
the number of characters to read, stdin means to read from the standard input which is the keyboard. strncmp(str1, str2, n) :it returns 0 if the first n characters of str1 is equal to the first n characters of str2, less than 0 if str1 < str2, and greater than 0 if str1 > str2. strncpy(str1, str2, n) This function is used to copy a string from another string. Copies the first n characters of str2 to str1 strchr(str1, c): it returns a pointer to the first occurrence of char c in str1, or NULL if character not found. strrchr(str1, c): it searches str1 in reverse and returns a pointer to the position of char c in str1, or NULL if character not found. strstr(str1, str2): it returns a pointer to the first occurrence of str2 in str1, or NULL if str2 not found. strncat(str1, str2, n) Appends (concatenates) first n characters of str2 to the end of str1 and returns a pointer to str1. strlwr() :to convert string to lower case strupr() :to convert string to upper case strrev() : to reverse string 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 (zero) 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. A string pointer declaration such as char *string = “language” is a constant and cannot be modified.