Strings
In C programming, strings can be defined as an array of characters terminating with "\0". Declaring strings is almost the same as declaring a one-dimensional array.
How to declare a string
Here is the basic syntax used to declare a string:
Char str_name[size];
Where
- Str_name – The name given to the string variable
- Size – It tells us the number of characters that the string will store. Size is the length of the string
It would be best if you never forgot that a string has an extra terminating character known as the Null character (\0). It is what differentiates strings from normal character arrays. The null character is used to indicate the termination of a string
How to initialize a string
There are several ways of initializing a string. Our c programming assignment help experts have explained it below using an example. They have declared a string with the name 'str' and initialized it with “programmingassignmenthelper.”
Char str[ ] = “programmingassignmenthelper”;
Char str [50] = “programmingassignmenthelper”;
Char str[ ] = { ‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘m’, ‘I’, ‘n’, ’g’, ‘a’, ‘s’, ‘s’, ‘I’, ‘g’, ‘n’, ‘m’, ‘e’, ‘n’, ‘t’, ’h’, ‘e’, ‘l’, ‘p’, ‘e’, ‘r’, ‘\0’};
Char str [29] = { ‘P’, ‘r’, ‘o’, ‘g’, ‘r’, ‘a’, ‘m’, ‘m’, ‘I’, ‘n’, ’g’, ‘a’, ‘s’, ‘s’, ‘I’, ‘g’, ‘n’, ‘m’, ‘e’, ‘n’, ‘t’, ’h’, ‘e’, ‘l’, ‘p’, ‘e’, ‘r’, ‘\0’};
The string “programming” can also be represented in memory as shown below:
0 1 2 3 4 5 6 7 8 9 10
P | r | o | g | r | a | m | m | i | N | g |
0x131 | 0x132 | 0x133 | 0x134 | 0x135 | 0x136 | 0x137 | 0x138 | 0x139 | 0x1310 | 0x1311 |
To help you have a clear understanding and grasp the concept of string declaration and initialization, we have prepared a sample program below:
// A C programming illustrating declaring and initializing strings
#include
Int main ( )
{
//Initialize and declare the string
charstr [ ] = “programming”;
//Next, print the string
printf (“%s”, str);
return 0;
}
The output will be
programming
From the sample program above, we can see that normal printf statements like in other variables also print strings. However, strings need not be printed character by character like in arrays. Although the C language does not have an inbuilt data type for strings, it has an access specifier, which is denoted by “%s.” This specifier can be used directly to read and print strings.
Here is another C program sample that can read a string from a user:
//Sample program that read strings
#include
Int main ( )
{
//Declare the string
charstr [30];
//reading the string
scanf ( “%s”, str);
//printing the string
printf (“%s”, str);
return 0;
}
In the sample above, a scanf statement has been used to read a string. Probably, you might also be wondering why we have not used the ampersand sign ‘&’ with the string name ‘str’. Well, you need to recall your knowledge of the scanf to understand this. If you are not new to C programming, then you know that the ampersand is used to provide the address of the scanf () function in order to store the value read in memory. Since str[ ] is a character array, using str without any braces, give the base address of this string. In our example, we are already providing the base address of the string to scanf. That is why we have not used the ‘&.’
How to pass strings to a function
Since strings are character arrays, they can be passed to a function. This can be done the same way we pass an array to a function. Written below is a program that passes a string to a function:
// C programming that shows how to pass a string to a function
#include
Void printStr (char str[])
{
Prinf (“The string is: %s”, str);
}
Int main ( )
{
// initializing and declaring the string
Char str [ ] = “programmingassignmenthelper”;
//printing the string by passing it to a different function
PrintStr (str);
Return 0;
}
When you run this program, the output will be:
The String is: programmingassignmenthelper
Get in touch with us if you need professional help with your c programming project. We boast of programming-savvy individuals who offer the best help.