Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
97 views

C Programming String

In C programming, strings are arrays of characters that are terminated with a null character ('\0'). Strings can be declared and initialized in several ways, such as character arrays ("abcd") or pointers ("abcd"). To read a string from the user, functions like scanf() and gets() can be used, with scanf() reading a word and gets() reading a full line of input. Various string handling functions defined in the string.h header file make common string operations like finding length or concatenating strings easier.

Uploaded by

slspa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views

C Programming String

In C programming, strings are arrays of characters that are terminated with a null character ('\0'). Strings can be declared and initialized in several ways, such as character arrays ("abcd") or pointers ("abcd"). To read a string from the user, functions like scanf() and gets() can be used, with scanf() reading a word and gets() reading a full line of input. Various string handling functions defined in the string.h header file make common string operations like finding length or concatenating strings easier.

Uploaded by

slspa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Programming String

In C programming, array of character are called strings. A string is terminated by null character /0 . For
example:
"c string tutorial"

Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of
string.

Declaration of strings
Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type.
char s[5];

Strings can also be declared using pointer.


char *p

Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','\0'};
OR;
char c[5]={'a','b','c','d','\0'};

String can also be initialized using pointers


char *c="abcd";
you will more about pointers in next chapter.

Reading Strings from user.


Reading words from user.
char c[20];
scanf("%s",c);

String variable c can only take a word. It is beacause when white space is encountered, the scanf() function
terminates.
Write a C program to illustrate how to read string from terminal.
#include <stdio.h>
int main(){
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s.",name);
return 0;
}

Output
Enter name: Dennis Ritchie
Your name is Dennis.

Here, program will ignore Ritchie because, scanf() function takes only string before the white space.

Reading a line of text


C program to read line of text manually.
#include <stdio.h>
int main(){
char name[30],ch;
int i=0;
printf("Enter name: ");
while(ch!='\n') // terminates if user hit enter
{
ch=getchar();
name[i]=ch;
i++;
}
name[i]='\0';
// inserting null character at end
printf("Name: %s",name);
return 0;
}

This process to take string is tedious. There are predefined functions gets() and puts in C language to read
and display string respectively.
int main(){
char name[30];
printf("Enter name: ");
gets(name);
//Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}

Both, the above program has same output below:

Output
Enter name: Tom Hanks
Name: Tom Hanks

String handling functions


You can perform different type of string operations manually like: finding length of string,
concatenating(joining) two strings etc. But, for programmers ease, many library function are defined under
header file <string.h> to handle these commonly used talk in C programming. You will learn more about
string hadling function in next chapter.

You might also like