Professional Documents
Culture Documents
OBJECTIVE:
This lab will introduce Strings in a C Language Program. At the end of this lab, you should be able to:
APPARATUS:
Laptop\PC with following tools installed
o Visual Studio Code with C/C++ and Code Runner Extensions
o C/C++ mingw-w64 tools for Windows 10
LAB SUBMISSION:
Create a Visual Studio Code Workspace, Lab_17 and c files (Task_17_1.c to Task_17_6.c) for
individual tasks and add them to Lab_17 workspace.
STRINGS
Strings are one-dimensional array of characters terminated by a null character '\0'. Thus, a null-terminated
string contains the characters that comprise the string followed by a null.
The following declaration and initialization create a string consisting of the word "Hello". To hold the null
character at the end of the array, the size of the character array containing the string is one more than the
number of characters in the word "Hello."
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'}; // simple string
Syntax to Declare a String of 5-Characters
If you follow the rule of array initialization, then you can write the above statement as follows:
Index 0 1 2 3 4 5
Variable 'H' 'e' 'l' 'l' 'o' '\0'
You do not place the null character at the end of a string constant. The C compiler automatically places the
'\0' at the end of the string when it initializes the array.
Arrays and Strings do not support assignment operator once it is declared. For example
char greeting[6];
greeting = {'H', 'e', 'l', 'l', 'o', '\0'}; // Error! Array type is not assignable
Error while Assigning an Array after Declaration
#include <stdio.h>
int main()
{
// initialize a simple string
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
return 0;
}
// End of program
Program Output Greeting message: Hello
We can use the scanf() function to read a string. The scanf() function reads the sequence of characters
until it encounters whitespace (space, newline, tab, etc.)
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
// End of program
Enter name: Ali Khan
Program Output
Your name is Ali.
In above example, even though Ali Khan was entered, but only Ali was stored in the name string. It is
because of the blank space between Ali and Khan.
Also notice that we have used the code name instead of &name with scanf(). This is because name is a char
array, and we know that arrays names decay to pointers in C. Thus, the name in scanf() already points to
the address of first element in the string, which is why we don’t need to us &.
We can use the fgets() function to read a line of string. And we can use puts() to display the string.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
fgets(name, sizeof(name), stdin); // read string
printf("Your name is ");
puts(name); // display string
return 0;
}
// End of program
Enter name: Ali Khan
Program Output
Your name is Ali Khan
In Example 13.3, we have used fgets() function to read a string from the user. The sizeof(name) results
to 30. Hence, we can take a maximum of 30 characters as input which is the size of the name string. To print
the string, we have used puts(name). Though, fgets() and puts() handles strings, both these functions
are defined in stdio.h header file.
#include <stdio.h>
int main()
{
char sentence[80], character;
int count = 0;
return 0;
}
// End of program
Answer:
Answer:
# Function Description
1 strcpy(s1, s2); Copies string s2 into string s1.
2 strcat(s1, s2); Concatenates string s2 onto the end of string s1.
3 strlen(s1); Returns the length of string s1.
4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1>s2; greater than 0 if s1<s2.
5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.
7 strlwr(s1); Returns the string with all lower-case letters of s1.
8 strupr(s1); Returns the string with all upper-case letters of s1.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[6] = "Hello";
char str2[6] = "World";
char str3[12];
int len = 0;
// concatenates str1 and str2 and store result back into str1
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1);
return 0;
}
// End of program
strcpy( str3, str2) : Hello
strcat( str1, str2): HelloWorld
Program Output
strlen(str1) : 10
strupr(str2) : WORLD
Answer:
#include <stdio.h>
// function declaration
int chFrequency(char str[], char character);
int main()
{
char str[100], ch;
int chFreq = 0;
// function call
chFreq = chFrequency(str, ch);
return 0;
}
// function definition
int chFrequency(char str[], char character)
{
int freq = 0;
return freq;
}
// End of program
Please enter the string: This is my first user-defined function
with string as parameter.
Program Output Please enter the character >>i
Answer:
string_length(string1): 15
Sample Output
string_copy (string3, string1): Quick Brown Fox
string_upper_case(str2): LAZY DOG
str4 = string_concatenate(string2, string1): Lazy Dog
Quick Brown Fox
Answer:
ARRAY OF STRINGS
We can also create an array of string. Each slot of the array will contain a string and that can be of any size
with possibility of different string size in each array location. Let us discuss the example below.
----------------------------------------------------------------------
Written by Shujat Ali (engrshujatali@gmail.com) on 21-Nov-2022.
IDE: Visual Studio Code 1.60.0
C Compiler: GCC (Rev. 5, Built by MSYS2 Project) 10.3.0 */
#include <stdio.h>
int main()
{
char planets[][8] = {"Mercury", "Venus", "Earth",
"Mars", "Jupiter", "Saturn",
"Uranus", "Neptune", "Pluto"};
char firstLetter = ' ';
int count = 0;
return 0;
}
// End of program
Please enter the first letter >> M
Program Output 1 Mercury begins with M
Mars begins with M
Please enter the first letter >> O
Program Output 2
No planet in Solar System starts with "O".
As you can see in the array of strings definition that number of rows of the planets array is omitted, as it is
obvious from the number of elements in the initializer, but we must specify the number of columns, i.e.,
maximum length of string.
The inefficiency that appears in this example is common when working with strings, since most collections
of strings will be a mixture of long strings and short strings. In C language, we can tackle this problem by
simulating “rugged array type” by creating an array whose elements are pointers to strings.
TASK 17.6: Lexicographical Order [1 point]
Write a program that take 7 different strings from user and display
them on screen in lexicographical (dictionary) order. You must define a
2-D array of string for this problem.
Please enter the string1: grapes
Please enter the string2: bananas
Please enter the string3: apples
Please enter the string4: peaches
Please enter the string5: mangoes
Sample Output
Please enter the string6: papayas
Please enter the string7: pears
Answer:
Students are advised to fill the manual and submit it before the upcoming lab. Kindly rename the file as
‘MCT-242L_CP1_2022_LM17_XX’, where XX is your roll number. After completing the manual, turn it in Google Classroom.