SlideShare a Scribd company logo
3
Most read
4
Most read
6
Most read
Strings
Contents
• 3.1 Declaration and initialization, string
input/output, format specifiers
• 3.2 Standard library functions
• 3.3 Strings and pointers
• 3.4 Array of strings
• 3.5 Command Line Arguments
What is String?
• A string is a sequence of characters
terminated with null character ‘0’.
• In C string is defined using array of characters.
• Example char name [] = “COMPUTER”;
C O M P U T E R ‘0’
String declaration
• Syntax : char string_name [no of characters];
example char s1[10];
char faculty[30];
String initialization
• Syntax : char string_name [] = “”;
example char s1[] = “c language”;
char faculty[] = “Arts”;
String input output
Input function : scanf / gets
Example
scanf(“%s”, s1);
gets(s1);
Output function: printf/puts
printf(“n s1= %s”, s1);
puts(s1);
Format Specifier
%s
Standard Library Functions
<string.h>
• strlen : returns length of the string
int strlen (const char *)
• strcpy : copy one string into another
void strcpy (char *, const char *)
strcpy(s1,s2);
• strcat : concantenates (combines) two strings
void strcat (char *, const char *)
strcat(s1,s2);
• strcmp : compare two strings
int strcmp (const char *, const char *)
res = strcmp (s1,s2);
// convert string in Upper case
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main()
{
char s[] = "computer";
printf("n string in upper case n");
for(int i; i< strlen(s);i++)
{
printf("%c",toupper(s[i]));
}
return 0;
}
// calculate length of the string without using std
function strlen
#include <stdio.h>
#include <string.h>
int main()
{
char s[30];
int i=0;
printf("n enter any string : ");
scanf("%s", s);
while(s[i] != '0')
i++;
printf("n length of string %s is %d",s,i);
return 0;
}
Pointers and Strings
Pointer to character array
char name[30];
char *p;
p = name;
void
Use pointers to store strings and
display strings
#include <stdio.h>
#include <string.h>
int main()
{
char *name[4] = { "vinita", "vinay", "seema", "arya“ };
int i,j;
for(i=0; i<4;i++)
{ j=0;
while(*(name[i] +j) != '0')
{
printf(“%c",*(name[i] +j));
j++;
}
printf("n");
}
return 0;
}
Array of Strings
A string is a 1-D array of characters, so an array of strings is a 2-D array of characters.
• char names[3][6] = {
{‘A', ‘r', ‘y', ‘a’},
{'t', 'o', 'm},
{'j', 'e', 'r', 'r', 'y'}
};
• char names[3][6] = {
“Arya”,
“tom”,
“jerry”
};
0 1 2 3 4 5
0 ‘A’ ‘r’ ‘y’ ‘a’ ‘0’
1 ‘t’ ‘o’ ‘m’ ‘0’
2 ‘j’ ‘e’ ‘r’ ‘r’ ‘y’ ‘0’
//Array of Strings
// Display data from array of strings
#include <stdio.h>
int main()
{
char names[3][20] = { "Rutuja",
"Mansi",
"Sushmita"
};
for(int i=0;i<3;i++)
printf("%sn",names +i);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char names[4][20];
char s[20];
int i;
printf("n enter any four names of students :");
for(i=0;i<4;i++)
{
scanf("%s",names +i);
}
printf("n The student's names aren");
for(i=0;i<4;i++)
printf("%sn",names +i);
printf("n enter the name to search in array :- ");
scanf("%s",s);
for(i=0;i<4;i++)
{
if(strcmp(s,names+i) == 0)
{
printf("n successful search...");
}
}
if(i >=4)
printf("n Unsuccessful search");
return 0;
}
Strings in C language

More Related Content

PPTX
Controller Area Network (CAN) Protocol || Automotive Electronics || Hariharan K
PPT
Array in c
PPTX
Strings in C
PPTX
Functions in c language
PPTX
C programming - String
PPTX
R Programming Language
PPTX
Arrays in c
PPT
Computer security overview
 
Controller Area Network (CAN) Protocol || Automotive Electronics || Hariharan K
Array in c
Strings in C
Functions in c language
C programming - String
R Programming Language
Arrays in c
Computer security overview
 

What's hot (20)

PPTX
Functions in c
PPTX
Pointers in C Programming
PPT
Operator Overloading
PPT
Function overloading(c++)
PPTX
PDF
Pointers in C
PPT
RECURSION IN C
PPTX
Pointer in c
PPT
FUNCTIONS IN c++ PPT
PPTX
Unit 3. Input and Output
PPTX
Input output statement in C
PPTX
Dynamic memory allocation in c
PPTX
File in C language
PPTX
classes and objects in C++
PPT
Class and object in C++
PPTX
Recursive Function
PPTX
String in c programming
PDF
Managing I/O in c++
PPT
Variables in C Programming
PPTX
C Programming: Control Structure
Functions in c
Pointers in C Programming
Operator Overloading
Function overloading(c++)
Pointers in C
RECURSION IN C
Pointer in c
FUNCTIONS IN c++ PPT
Unit 3. Input and Output
Input output statement in C
Dynamic memory allocation in c
File in C language
classes and objects in C++
Class and object in C++
Recursive Function
String in c programming
Managing I/O in c++
Variables in C Programming
C Programming: Control Structure
Ad

Similar to Strings in C language (20)

PPTX
introduction to strings in c programming
PPTX
UNIT 4C-Strings.pptx for c language and basic knowledge
PDF
Principals of Programming in CModule -5.pdfModule-4.pdf
PPTX
Module-2_Strings concepts in c programming
PPTX
programming for problem solving using C-STRINGSc
PDF
c ppt.pdf
PPT
String manipulation techniques like string compare copy
PPTX
Presentation more c_programmingcharacter_and_string_handling_
PDF
SPL 13 | Character Array(String) in C
PDF
String
PPTX
Programming in C - Fundamental Study of Strings
DOCX
Unitii string
DOCX
C Programming Strings.docx
PPTX
Lecture 2. mte 407
PPTX
C-Arrays & Strings (computer programming).pptx
PDF
Data structure week 3
PDF
Strings IN C
PPTX
Week6_P_String.pptx
PPT
14 strings
introduction to strings in c programming
UNIT 4C-Strings.pptx for c language and basic knowledge
Principals of Programming in CModule -5.pdfModule-4.pdf
Module-2_Strings concepts in c programming
programming for problem solving using C-STRINGSc
c ppt.pdf
String manipulation techniques like string compare copy
Presentation more c_programmingcharacter_and_string_handling_
SPL 13 | Character Array(String) in C
String
Programming in C - Fundamental Study of Strings
Unitii string
C Programming Strings.docx
Lecture 2. mte 407
C-Arrays & Strings (computer programming).pptx
Data structure week 3
Strings IN C
Week6_P_String.pptx
14 strings
Ad

Recently uploaded (20)

PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Modernizing your data center with Dell and AMD
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Machine learning based COVID-19 study performance prediction
PDF
KodekX | Application Modernization Development
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Understanding_Digital_Forensics_Presentation.pptx
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...
The AUB Centre for AI in Media Proposal.docx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Unlocking AI with Model Context Protocol (MCP)
Modernizing your data center with Dell and AMD
Review of recent advances in non-invasive hemoglobin estimation
Machine learning based COVID-19 study performance prediction
KodekX | Application Modernization Development
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
CIFDAQ's Market Insight: SEC Turns Pro Crypto
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Strings in C language

  • 2. Contents • 3.1 Declaration and initialization, string input/output, format specifiers • 3.2 Standard library functions • 3.3 Strings and pointers • 3.4 Array of strings • 3.5 Command Line Arguments
  • 3. What is String? • A string is a sequence of characters terminated with null character ‘0’. • In C string is defined using array of characters. • Example char name [] = “COMPUTER”; C O M P U T E R ‘0’
  • 4. String declaration • Syntax : char string_name [no of characters]; example char s1[10]; char faculty[30]; String initialization • Syntax : char string_name [] = “”; example char s1[] = “c language”; char faculty[] = “Arts”;
  • 5. String input output Input function : scanf / gets Example scanf(“%s”, s1); gets(s1); Output function: printf/puts printf(“n s1= %s”, s1); puts(s1); Format Specifier %s
  • 6. Standard Library Functions <string.h> • strlen : returns length of the string int strlen (const char *) • strcpy : copy one string into another void strcpy (char *, const char *) strcpy(s1,s2); • strcat : concantenates (combines) two strings void strcat (char *, const char *) strcat(s1,s2); • strcmp : compare two strings int strcmp (const char *, const char *) res = strcmp (s1,s2);
  • 7. // convert string in Upper case #include <stdio.h> #include <string.h> #include <ctype.h> int main() { char s[] = "computer"; printf("n string in upper case n"); for(int i; i< strlen(s);i++) { printf("%c",toupper(s[i])); } return 0; }
  • 8. // calculate length of the string without using std function strlen #include <stdio.h> #include <string.h> int main() { char s[30]; int i=0; printf("n enter any string : "); scanf("%s", s); while(s[i] != '0') i++; printf("n length of string %s is %d",s,i); return 0; }
  • 9. Pointers and Strings Pointer to character array char name[30]; char *p; p = name; void
  • 10. Use pointers to store strings and display strings #include <stdio.h> #include <string.h> int main() { char *name[4] = { "vinita", "vinay", "seema", "arya“ }; int i,j; for(i=0; i<4;i++) { j=0; while(*(name[i] +j) != '0') { printf(“%c",*(name[i] +j)); j++; } printf("n"); } return 0; }
  • 11. Array of Strings A string is a 1-D array of characters, so an array of strings is a 2-D array of characters. • char names[3][6] = { {‘A', ‘r', ‘y', ‘a’}, {'t', 'o', 'm}, {'j', 'e', 'r', 'r', 'y'} }; • char names[3][6] = { “Arya”, “tom”, “jerry” }; 0 1 2 3 4 5 0 ‘A’ ‘r’ ‘y’ ‘a’ ‘0’ 1 ‘t’ ‘o’ ‘m’ ‘0’ 2 ‘j’ ‘e’ ‘r’ ‘r’ ‘y’ ‘0’
  • 12. //Array of Strings // Display data from array of strings #include <stdio.h> int main() { char names[3][20] = { "Rutuja", "Mansi", "Sushmita" }; for(int i=0;i<3;i++) printf("%sn",names +i); return 0; }
  • 13. #include <stdio.h> #include <string.h> int main() { char names[4][20]; char s[20]; int i; printf("n enter any four names of students :"); for(i=0;i<4;i++) { scanf("%s",names +i); } printf("n The student's names aren"); for(i=0;i<4;i++) printf("%sn",names +i); printf("n enter the name to search in array :- "); scanf("%s",s); for(i=0;i<4;i++) { if(strcmp(s,names+i) == 0) { printf("n successful search..."); } } if(i >=4) printf("n Unsuccessful search"); return 0; }