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

C strings

The document provides an overview of C strings, explaining how to create, modify, and access strings using character arrays. It covers various methods for assigning string literals, reading strings with whitespace, and passing strings to functions. Additionally, it highlights common string functions in C for manipulating strings effectively.

Uploaded by

daniruddha003
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)
5 views

C strings

The document provides an overview of C strings, explaining how to create, modify, and access strings using character arrays. It covers various methods for assigning string literals, reading strings with whitespace, and passing strings to functions. Additionally, it highlights common string functions in C for manipulating strings effectively.

Uploaded by

daniruddha003
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/ 7

RAMKRISHNA MAHATO GOVERMENT ENGINEERING

COLLEGE , PURULIA

NAME – AMIT DAS


ROLL NO. - 35000324056
Dept.- Electronics & Communication
Engineering
Year - 1st
Session – 2024-25
Subject – C Programming

C Strings
Introduction
Strings are used for storing
text/characters.
For example, "Hello World" is a char greetings[] = "Hello World!";

string of characters.
Example:
Unlike many other programming char greetings[]= "Hello World!";

languages, C does not have a String printf("%s", greetings);

type to easily create string


variables. Instead, you must use the
char type and create an array of
characters to make a string in C:
Note that you have to use double
quotes ("").
To output the string, you can use
the printf() function together with
the format specifier %s to tell C
that we are now working with
strings:

Example:
char greetings[] = "Hello World!";
printf("%c", greetings[0]);
Access Strings
Example:
Since strings are actually arrays in
char greetings[] = "Hello World!";
C, you can access a string by greetings[0] = 'J';

referring to its index number inside printf("%s", greetings);


// Outputs Jello World! instead of Hello
square brackets [].
World!
This example prints the first
Example:
character (0) in greetings:
char carName[] = "Volvo";
int i;

Modify Strings for (i = 0; i < 5; ++i) {


printf("%c\n", carName[i]);
To change the value of a specific }

character in a string, refer to the


index number, and use single
char str[] = "GeeksforGeeks";
quotes:

Loop Through a
String
char str[50] = "GeeksforGeeks";
You can also loop through the
characters of a string, using a for
loop:
Assigning a String
Literal without Size
char str[14] = {
String literals can be assigned 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};

without size. Here, the name of the


string str acts as a pointer because
it is an array.

Assigning a String
Literal with a
char str[] = {
Predefined Size 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};

String literals can be assigned with


a predefined size. But we should
always account for one extra space
which will be assigned to the null
character. If we want to store a
string of size n then we should
Example of String Input using gets():
always declare a string with a size // C program to illustrate
// fgets()
equal to or greater than n+1.
#include <stdio.h>
#define MAX 50

Assigning Character int main()


{
by Character with char str[MAX];
// MAX Size if 50 defined
Size fgets(str, MAX, stdin);
We can also assign a string printf("String is: \n");
// Displaying Strings using Puts
character by character. But we
puts(str);
should remember to set the end return 0;
}
character as ‘\0’ which is a null
Input:
character. “GeeksforGeeks”
Output:
String is:
Assigning Character “GeeksforGeeks”

by Character without
Size
We can assign character by // C program to illustrate how to

character without size with the // pass string to functions


#include <stdio.h>
NULL character at the end. The size
void printStr(char str[]) { printf("String is :
of the string is determined by the %s", str); }
int main()
compiler automatically.
{
// declare and initialize string

Read a String char str[] = "GeeksforGeeks";


// print string by passing string

Separated by // to a different function


printStr(str);
Whitespaces in C return 0;
}
We can use multiple methods to
Output:
read a string separated by spaces in String is : GeeksforGeeks
C. The two of the common ones
are:
We can use the fgets() function to
read a line of string and gets() to
read characters from the standard
input (stdin) and store them as a C
string until a newline character or
the End-of-file (EOF) is reached.
We can also scanset characters
inside the scanf() function.

C String Length
The length of the string is the
number of characters present in
the string except for the NULL
character. We can easily find the
length of the string using the loop
to count the characters from the
start till the NULL character is
found.
Passing Strings to
Function
As strings are character arrays, we
can pass strings to functions in the
same way we pass an array to a
function.

Conclusion
String functions in C provide a
convenient and efficient way to
manipulate strings. These
functions, such as strlen(), strcpy(),
strcat(), and strcmp(), allow for
common string operations like
finding their length, copying them,
concatenating them, and
comparing them. Careful use of
these functions can lead to more
robust and efficient programs.

You might also like