Course Content
Detailed Content of Programming in C
0/1
Introduction
0/1
Structure of C program
0/1
Answers of ‘C’ Pointers Programs
0/1
About Lesson

String: 

A String is a single character treated as a single unit. A String may include letters, digits and various special characters such as +, -, *, / and $. Strings literals or string constants in c are written in double quotation marks as follows:

“1000 Main Street”    (a street address)

“(080)329-7082”        (a telephone number)

In C, language strings are stored in array of char type along with null terminating character ” at the end.

1. Declaration and Initialization:

Strings can be declared and initialized using an array of characters:

 

#include <stdio.h>

int main() {
// Declaration and initialization of a string
char greeting[] = “Hello, World!”;

// Alternatively, you can declare a string without initialization and later assign a value
// char greeting[20];
// strcpy(greeting, “Hello, World!”);

// Printing the string
printf(“%sn”, greeting);

return 0;
}

 

2. Processing Strings:

a. Inputting a String:

#include <stdio.h>

int main() {
char name[50];

// Inputting a string
printf(“Enter your name: “);
scanf(“%s”, name);

// Printing the inputted string
printf(“Hello, %s!n”, name);

return 0;
}

b. String Functions:

C provides standard library functions for working with strings. Here are a few examples:

#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = “Hello”;
char str2[] = “, World!”;

// Concatenating strings using strcat
strcat(str1, str2);
printf(“Concatenated string: %sn”, str1);

// Copying strings using strcpy
char copy[20];
strcpy(copy, str1);
printf(“Copied string: %sn”, copy);

// Finding the length of a string using strlen
printf(“Length of str1: %lun”, strlen(str1));

// Comparing strings using strcmp
char cmpStr[] = “Hello, World!”;
if (strcmp(str1, cmpStr) == 0) {
printf(“str1 is equal to cmpStr.n”);
} else {
printf(“str1 is not equal to cmpStr.n”);
}

return 0;
}

 

Important Note:

When working with strings in C, it’s crucial to ensure that the array is large enough to accommodate the string and the null character. Failure to do so can lead to buffer overflows and undefined behavior. The null character ('') is used to mark the end of the string.


Reading Strings:

If we declare a string by writing

char str[100];

then ‘str‘ can be read from the user by using three ways;

  1. Using scanf() function
  2. Using  gets() function
  3. Using getchar(), getch(), or getche() function repeatedly

The ‘String‘ can be read using scanf() by writing

scanf(“%s”, str);

Although the syntax of scanf() function is well known and easy to use, the main pitfall with this function is that it terminates as soon as it finds a blank space.

For example, if the user enters “Hello World“, then str will contain only Hello. This is because, the moment a blank space is encountered, the moment a blank space is encountered, the string is terminated by the scanf() function.

Example: 

char str[10];

printf(“enter a stringn”);

gets(str);

The String can also be read by calling the getchar() repeatedly to read a sequence of single characters( unless a terminating characters is encountered) and simultaneously storing it in a character array as follows:

int i=0;

char str[10], ch;

getchar(ch);

while(ch);

{

str[i]=ch;       //store the read character

in str i++;

getch(ch);     //get another character

}

str[i]=’10’;   //terminate str with null character

Writing Strings:

The String can be displayed on screen using 3 ways:

  1. Using printf() function
  2. Using puts() function
  3. Using putchar() function repeatedly

The String can be displayed using printf()

printf(“%s”, str);