Holla Tech - Learn

Strings
 

A string in C is an array of characters that ends with a NULL character ‘\0’.
A string declaration can be made in several ways, each with its own considerations.
For example: 

char str_name[str_len] = “string”

 

This creates a string named str_name of str_len characters and initializes it to the value “string”.
When you provide a string literal to initialize the string, the compiler automatically adds a NULL character ‘\0’ to the char array.
For this reason, you must  declare the array size to be at least one character longer than the expected string length.
The statements below creates strings that include the NULL character. If the declaration does not include a char array size, then it will be calculated based on the length of the string in the initialization plus one for ‘\0’:

char str1[6] = “hello”;
char str2[ ] = “world”;  /* size 6 */ 

 

A string can also be declared as a set of characters:

char str3[6] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};

char str4[ ] = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’}; /* size 6 */ 

 

With this approach, the NULL character must be added explicitly. Note that the characters are enclosed in single quotation marks.
As with any array, the name of a string acts as a pointer.
A string literal is a text enclosed in double quotation marks.
A character, such as ‘b’, is indicated by single quotation marks and cannot be treated as a string.

A string pointer declaration such as char *str = “stuff”; is considered a constant and cannot be changed from its initial value.

To safely and conveniently operate with strings, you can use the Standard Library string functions shown below. Don’t forget to include <string.h>.
strlen() – get length of a string
strcat() – merge two strings
strcpy() – copy one string to another
strlwr() – convert string to lower case
strupr() – conver string to upper case
strrev() – reverse string
strcmp() – compare two strings

NOTE!
String functions and string pointers are covered in later lessons.

String Input
 

Programs are often interactive, asking the user for input.
To retrieve a line of text or other string from the user, C provides the scanf(), gets(), and fgets() functions.
You can use scanf() to read input according to the format specifiers.
For example:

char first_name[25];
int age;
printf(“Enter your first name and age: \n”);
scanf(“%s %d”, first_name, &age); 

 

When scanf() is used to read a string, there is no need for & to access the variable address because an array name acts as a pointer.
scanf() stops reading input when it reaches a space. To read a string with spaces, use the gets() function. It reads input until a terminating newline is reached (the Enter key is pressed).
For example:

char full_name[50];
printf(“Enter your full name: “);
gets(full_name); 

 

A safer alternative to gets() is fgets(), which reads up to a specified number of characters. This approach helps prevent a buffer overflow, which happens when the string array isn’t big enough for the typed text.
For example:

char full_name[50];
printf(“Enter your full name: “);
fgets(full_name, 50, stdin); 

 

The fgets() arguments are the string name, the number of characters to read, and a pointer to where you want to read the string from. stdin means to read from the standard input, which is the keyboard.
Another difference between gets and fgets is that the newline character is stored by fgets.

NOTE!
fgets() reads only n-1 characters from stdin because there must be room for ‘\0’.

String Output
 

String output is handled with the fputs(), puts(), and printf() functions.
The fputs() requires the name of the string and a pointer to where you want to print the string. To print to the screen, use stdout which refers to the standard output.
For example:

#include <stdio.h>
int main()
{
  char city[40];
  printf(“Enter your favorite city: “);
  gets(city);
  // Note: for safety, use
  // fgets(city, 40, stdin);

  fputs(city, stdout);
  printf(” is a fun city.”);

  return 0;

 

The puts() function takes only a string argument and can also be used to display output. However, it adds a newline to output.
For example:

#include <stdio.h>
int main()
{
  char city[40];
  printf(“Enter your favorite city: “);
  gets(city);
  // Note: for safety, use
  // fgets(city, 40, stdin);

  puts(city);

  return 0;

 


BACK NEXT

CLICK ON THE BUTTON BELOW TO GO TO THE C MAIN COURSE PAGE. 

C MAIN COURSE PAGE

 


© License: All Rights Reserved 


CONTACT HOLLA TECH – LEARN SUPPORT