0% found this document useful (0 votes)
23 views

STRINGS

The document provides an overview of strings in C programming, defining a string as an array of characters terminated by a null character. It covers the declaration, initialization, and reading of strings, along with examples of using functions like scanf(), getchar(), and gets(). Additionally, it explains string functions such as strlen(), strcpy(), strcat(), and strcmp(), including examples of their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

STRINGS

The document provides an overview of strings in C programming, defining a string as an array of characters terminated by a null character. It covers the declaration, initialization, and reading of strings, along with examples of using functions like scanf(), getchar(), and gets(). Additionally, it explains string functions such as strlen(), strcpy(), strcat(), and strcmp(), including examples of their usage.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

STRINGS

C LANGUAGE LAB
DEFINITION OF STRING

► In C programming, array of characters is


called a string. A string is terminated by a
null character \0.
► Example: c string tutorial
DECLARATION OF STRINGS

► Strings are declared in a similar manner as


arrays. Only difference is that, strings are of
char type.
► Using arrays
char s[5];
INITIALIZATION OF STRINGS

► char c[] = “abcd”;


► char c[50] = “abcd”;
► char c[] = {‘a’,’b’,’c’,’d’,’\0’};
► char c[5] = {‘a’,’b’,’c’,’d’,’\0’};
► char c[5] = "abcde";
//Here, we are trying to assign 6 characters (the last
character is '\0') to a char array having 5 characters.
This is bad and you should never do this.
► char c[100];
c = "C programming";
// Error! array type is not assignable.
READING STRINGS FROM USER

► You can use the scanf() function to read a


string like any other data types.
► However, the scanf() function only takes the
first entered word. The function terminates
when it encounters a white space (or just
space).
Example 1: Using scanf() to read a
string

#include<stdio.h>
#include<conio.h>
void main(){
clrscr();
char name[20];
printf(“Enter your name: ”);
scanf(“%s”, &name);
printf(“Your name is %s ”,name);
getch();
}
Example 2: Using getchar() to read a
line of text

#include<stdio.h>
#include<conio.h>
void main(){
char name[30], ch;
int i = 0;
printf(“Enter your name: “);
while(ch != ‘\n’){
ch = getchar();
name[i] = ch;
i++;
}
name[i] = ‘\0’;
printf(“Your name is %s”,name);
getch();
}
Example 3: Using standard library
function to read a line of text

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
char name[30];
printf(“Enter a name: ”);
gets(name);
printf(“Name: ”);
puts(name);
getch();
}
STRING FUNCTIONS

► strlen() = calculates the length of string


► strcpy() = copies a string to another string
► strcat() = concatenate(join) two strings
► strcmp() = compares two string
ACCESSING THE ELEMENTS OF A
STRING
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main(){
clrscr();
int i;
char name[30];
printf(“Enter a name: ”);
gets(name);
for (i=0; i<strlen(name-1);i++){
printf(“%c ”,name[i]);
}
getch();
}
Example: strcpy();
#include<stdio.h>
#include<conio,h>
#include<string.h>
void main(){
clrscr();
char str1[10]= "awesome";
char str2[10];
char str3[10];
strcpy(str2, str1);
strcpy(str3, "well");
puts(str2);
puts(str3);
getch();
}
Example strcat()

#include<stdio.h>
#include<conio,h>
#include<string.h>
void main()
{
char str1[] = "This is ", str2[] = "programiz.com";
strcat(str1,str2);
puts(str1);
puts(str2);
getch();
}
Example strcmp()
#include<stdio.h>
#include<conio,h>
#include<string.h>
void main()
{
char str1[] = “abcd”;
char str2[] = “abbd”;
char str3[] = “abcd”;
int result;
result = strcmp(str1, str2);
printf(“str1 compare str2 result is %d \n”,result);
result = strcmp(str1, str3);
printf(“str1 compare str3 result is %d \n”,result);
getch();
}

You might also like