0% found this document useful (0 votes)
17 views15 pages

Understanding Strings in C Programming

The document provides an overview of strings in C, explaining that strings are treated as one-dimensional arrays of characters and are terminated by a null character. It covers string initialization, reading, writing, and various string operations such as length calculation, copying, concatenation, and comparison. Additionally, it introduces structures in C as a user-defined data type for grouping different types of items.

Uploaded by

suni
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)
17 views15 pages

Understanding Strings in C Programming

The document provides an overview of strings in C, explaining that strings are treated as one-dimensional arrays of characters and are terminated by a null character. It covers string initialization, reading, writing, and various string operations such as length calculation, copying, concatenation, and comparison. Additionally, it introduces structures in C as a user-defined data type for grouping different types of items.

Uploaded by

suni
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

Strings

String is a sequence of characters that is treated


as a single data item and terminated by null
character ‘\0’.
C language does not support strings as a data
type.
A string is one dimensional array of characters in

C language.
Example: char
n name[]={‘n’,’a’,’m’,’e’,’\0’};
a m e
5001 5002 5003 5004
Char c[]=“c string”;\\space will be consider
c s t r i n g \0
Initialization of string:
Char c[]=“name”;
Char c[50]=“name”;
Char c[]={‘n’,’a’,’m’,’e’,’\0’};
Char c[5]={‘n’,’a’,’m’,’e’,’\0’};
The initialization of NULL character is not essential
because the C compiler inserts the NULL the (\0)
character automatically at the end of the string.
Reading the strings
String can read in three ways:
[Link]() function [Link]() function
[Link]() or getch() function.
Scanf():this is commonly used for accepting string.
Syntax: scanf(“%s” ,name_of _string_variable);
#include<stdio.h>
int main()
{ char name[20];
printf(“enter name”);
scanf(“%s”,name);
printf(“your name is %s”,name);
return 0;
}
gets() function
It is used to read string if it is contain spaces. Reads characters from the
standard input(stdin) and stores them as a C strings into str until a new line
character or end of file is reached.
Syntax: char *gets(char *str);
#include<stdio.h>
int main()
{
char z[100];
printf(“enter a string:\n”);
gets(z);
printf(“the string:%s\n”,z);
return 0;
}
fgets() function

fgets() function:it is used to read a line of string


Syntax:fgets(name,sizeof(name),stdin);//read string
{
char name[30];
printf(enter name”):
fgets(name,sizeof(name),stdin);//read string
printf(“name”);
puts(name);
return 0;
}
Writing strings
The string or character array can be displayed on screen using again three ways:
[Link]() function [Link]() function:
[Link]() function
Passing stings to functions:
void displayString(char str[])
int main()
{
char str[50];
printf(“enter string”);
fgets(str,sizeof(str),stdin);
displayString(str);\\passing a string to function
Return 0;
}
void displayString(char str[])
{
Printf(“string output”);puts(str);}
String Operations/Functions
String length
The strlen() function calculates the length of a given string. It doesn’t count the null
character ‘\0’.
Syntax: int strlen(const char *str); //str: It represents the string variable whose
length we have to find.
#include<stdio.h>
int main()
{
int length;
char s[20] = “welcome to c lab”;
length=strlen(s);
printf(“\Length of the string is = %d \n”, length);
return 0;
}
Length of the string is = 16
String Copy
If you have to copy the content of one string to another string, then this function is being [Link]
the null characters are copied in the process. The function can copy the content of one string to
another.
Syntax of the function is char strcpy(char* dest, const char* src);
#include <stdio.h>
#include <string.h>
int main()
{ // defining strings
char source[] = “programming lab";
char dest[20];
strcpy(dest, source); // Copying the source string to dest
printf("Source: %s\n", source); // // printing result
printf("Destination: %s\n", dest);
return 0;
}
Output:source: programming lab
Destination: programming lab
String Concatination
It takes two strings as input and concatenates the second string to the first string, which
means it will attach the second string to the end of the first string and save the
concatenated string to the first string. The size of the first string should be large
enough to save the result.
Syntax: char* strncat(char* dest, char* src, size_t n);
#include <stdio.h>
#include <string.h>
int main() {
char string1[10] = "Hello";
char string2[10] = "World";
strcat(string1, string2);
printf("Output string after concatenation: %s", string1);
Output string after concatenation: HelloWorld
#include <stdio.h>
#include <string.h>
int main () { char string1[10] = "Hello";
char string2[10] = "World";
strncat(string1,string2, 3);
printf("Concatenation using strncat: %s", string1); }
Out put: Concatenation using strncat: HelloWor
String comparision
The strcmp() compares two strings character by character. If the strings are equal, the function
return 0.
#include<stdio.h>
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
Output: Enter 1st string: hello
Enter 2nd string: hello
Strings are equal
Reverse String

The strrev(string) function returns reverse of the given string.


#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
Enter string: welcome
String is: welcome
Reverse String is: emoclew
WITHOUT LIBRARY FUNCTIONS
• C Program we have to accept two strings from user using gets and we have to concatenate these two
strings without using library functions .
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
int main() {
char s1[50], s2[30]; printf("\nEnter String 1 :");
gets(s1);
printf("\nEnter String 2 :");
gets(s2);
concat(s1, s2);
printf("\nConcated string is :%s", s1);
return (0);
}
void concat(char s1[], char s2[]) {
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '\0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '\0';
}
Output:Enter String 1 : COMPUTER
Enter String 2 : PROGRAMMING
Concated string is : COMPUTERPROGRAMMING
Copy two strings without library functions
#include <stdio.h>
#include <string.h>
int main()
{ char Str[100], CopyStr[100];
int i;
printf("\n Please Enter any String : ");
gets(Str);
for (i = 0; Str[i]!='\0'; i++)
{
CopyStr[i] = Str[i]; }
CopyStr[i] = '\0';
printf("\n String that we coped into CopyStr = %s", CopyStr);
printf("\n Total Number of Characters that we copied = %d\n", i);
return 0;
}
Structure and Unios
The structure in C is a user-defined data type that can be used to
group items of possibly different types into a single type.
The struct keyword is used to define the structure in the C
programming language. The items in the structure are called
its member .
• In structure declaration, we specify its member variables
along with their datatype. We can use the struct keyword to
declare the structure in C using the following syntax:
• Syntax
• struct structure_name { data_type member_name1;
• data_type member_name1; .... .... }; and they can be of any
valid data type

You might also like