SlideShare a Scribd company logo
1 of 19
Structured Programming Language
Strings in C
Mohammad Imam Hossain,
Lecturer, CSE, UIU
String
• A String is an array of characters terminated
by a special symbol named as null character
(represented as ‘0’ )
• Null character is also named as string-
termination character and used to identify
the length of the string.
• Logical illustration of a string consisting of n
characters:
X[0] X[1] X[2] … … … X[n-1] ‘0’
Defining a String
char string_name[expression];
• string_name  user chosen name for the
string
• expression integer expression indicates
the number of elements in the string,
including the null character
Sample Code:
char name[50];
String vs Char Array
Operations string char array
declaration char name[50]; ///max 49 characters char name[50]; ///max 50 characters
initialization char name[50]=“SPL";
or,
char name[50]={'S','P','L','0'};
or,
char name[50]={'S','P','L'};
char name[50]={'S','P','L'};
Reading a String
string char array
char str[50];
scanf("%s", str);
Or,
gets(str);
Or,
scanf("%[^n]", str);
char str[50];
int sz;
printf("Enter the size: ");
scanf("%d",&sz);
int i;
for(i=0;i<sz;i++){
scanf("%c",&str[i]);
}
Writing a String
string char array
char str[50];
printf("String : %sn",str);
Or,
puts(str);
char str[50];
for(i=0;i<sz;i++){
printf("%c",str[i]);
}
printf("n");
String I/O character wise
char str[50];
///reading string character wise
int i=0;
do{
scanf("%c",&str[i]);
i=i+1;
}while(str[i-1]!='n');
str[i-1]='0';
///writing string character wise
i=0;
while(str[i]!='0'){
printf("%c",str[i]);
i=i+1;
}
printf("n");
String Processing
• Way 1 using some predefined functions with help of
<string.h> header file
• Way 2 processing all characters individually
String Length
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
int i=0;
while(str[i]!='0'){
i++;
}
int length=i;
printf("Length %dn",length);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
int length=strlen(str);
printf("Length %dn",length);
return 0;
}
String Concatenation
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50],src_string[30];
gets(dest_string);
gets(src_string);
printf("%sn",dest_string);
int len1=strlen(dest_string);
int i=0;
while(src_string[i]!='0'){
dest_string[len1+i]=src_string[i];
i++;
}
dest_string[len1+i]='0';
printf("%sn",dest_string);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50],src_string[30];
gets(dest_string);
gets(src_string);
printf("%sn",dest_string);
strcat(dest_string,src_string);
printf("%sn",dest_string);
return 0;
}
String Copy
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50]="hello...",src_string[30];
gets(src_string);///world
int i=0;
while(src_string[i]!='0'){
dest_string[i]=src_string[i];
i++;
}
dest_string[i]='0';
printf("%s",dest_string);///world
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char dest_string[50]="hello…",src_string[30];
gets(src_string);///world
strcpy(dest_string,src_string);
printf("%s",dest_string);///world
return 0;
}
String Compare
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str1[50],str2[50];
gets(str1);
gets(str2);
int i=0;
while(str1[i]!='0' && str2[i]!='0'){
i++;
}
int cmp=str1[i]-str2[i];
if(cmp<0){
printf("str2 is greatern");
}
else if(cmp>0){
printf("str1 is greatern");
}
else if(cmp==0) {
printf("Same stringn");
}
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str1[50],str2[50];
gets(str1);
gets(str2);
if(strcmp(str1,str2)<0){
printf("str2 is greatern");
}
else if(strcmp(str1,str2)>0){
printf("str1 is greatern");
}
else if(strcmp(str1,str2)==0) {
printf("Same stringn");
}
return 0;
}
LowerCase Conversion
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
int i=0;
while(str[i]!='0'){
if(str[i]>='A' && str[i]<='Z'){
str[i]='a'+str[i]-'A';
}
i++;
}
printf("%sn",str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
strlwr(str);
printf("%sn",str);
return 0;
}
UpperCase Conversion
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
strupr(str);
printf("%sn",str);
return 0;
}
Reversing a String
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
int len=strlen(str);
int first=0;
int last=len-1;
while(first<last){
int tmp=str[first];
str[first]=str[last];
str[last]=tmp;
first++;
last--;
}
printf("%sn",str);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("%sn",str);
strrev(str);
printf("%sn",str);
return 0;
}
String to integer
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("string: %sn",str);
int digit=0;
int i=0;
while(str[i]!='0'){
digit=digit*10+str[i]-'0';
i++;
}
printf("integer: %dn",digit);
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
gets(str);
printf("string: %sn",str);
int digit=atoi(str);
printf("integer: %dn",digit);
return 0;
}
Integer to string
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
void int_to_str(int val,char str[], int base)
{
int index=0;
while(val!=0)
{
int rem=val%base;
if(rem>9)
{
rem='a'+rem-10;
str[index]=rem;
}
else str[index]='0'+rem;
val=val/base;
index++;
}
str[index]='0';
strrev(str);
return ;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
int num;
scanf("%d",&num);
printf("integer: %dn",num);
itoa(num,str,10);///value, string name, base of
conversion system
printf("decimal string: %sn",str);
itoa(num,str,2);///converting into binary
printf("binary string: %sn",str);
itoa(num,str,8);///converting into octal
printf("octal string: %sn",str);
itoa(num,str,16);///converting into hexadecimal
printf("hexadecimal string: %sn",str);
return 0;
}
Integer to string
Character wise processing Header file <string.h>
#include <stdio.h>
#include <string.h>
void int_to_str(int val,char str[], int base)
{
int index=0;
while(val!=0)
{
int rem=val%base;
if(rem>9)
{
rem='a'+rem-10;
str[index]=rem;
}
else str[index]='0'+rem;
val=val/base;
index++;
}
str[index]='0';
strrev(str);
return ;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str[50];
int num;
scanf("%d",&num);
printf("integer: %dn",num);
itoa(num,str,10);///value, string name, base of
conversion system
printf("decimal string: %sn",str);
itoa(num,str,2);///converting into binary
printf("binary string: %sn",str);
itoa(num,str,8);///converting into octal
printf("octal string: %sn",str);
itoa(num,str,16);///converting into hexadecimal
printf("hexadecimal string: %sn",str);
return 0;
}
int main()
{
char str[50];
int num,base;
printf("Enter number and base to convert: ");
scanf("%d %d",&num,&base);
printf("integer: %dn",num);
int_to_str(num,str,base);
printf("string: %s",str);
return 0;
}
References
• Programming with C, Schaum’s outline, 3rd edition,
chapter 10
• https://www.tutorialspoint.com/cprogramming/c_str
ings.htm

More Related Content

What's hot (20)

Strings CPU GTU
Strings CPU GTUStrings CPU GTU
Strings CPU GTU
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
Strings
StringsStrings
Strings
 
14 strings
14 strings14 strings
14 strings
 
Kotlin for Android Developers - 2
Kotlin for Android Developers - 2Kotlin for Android Developers - 2
Kotlin for Android Developers - 2
 
Manipulating strings
Manipulating stringsManipulating strings
Manipulating strings
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
The string class
The string classThe string class
The string class
 
Strings in c
Strings in cStrings in c
Strings in c
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
 
String in c programming
String in c programmingString in c programming
String in c programming
 
C Language Unit-3
C Language Unit-3C Language Unit-3
C Language Unit-3
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfiles
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Python programming : Standard Input and Output
Python programming : Standard Input and OutputPython programming : Standard Input and Output
Python programming : Standard Input and Output
 
MySQL 5.7 String Functions
MySQL 5.7 String FunctionsMySQL 5.7 String Functions
MySQL 5.7 String Functions
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
 
Python strings presentation
Python strings presentationPython strings presentation
Python strings presentation
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 

Similar to SPL 13 | Character Array(String) in C

strings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfstrings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfHEMAHEMS5
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxAbhimanyuChaure
 
Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_KarthicaMarasamy
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.Samsil Arefin
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functionsAwinash Goswami
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdfJoyPalit
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderSamsil Arefin
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx8759000398
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTSasideepa
 

Similar to SPL 13 | Character Array(String) in C (20)

Strings in C
Strings in CStrings in C
Strings in C
 
strings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdfstrings-150319180934-conversion-gate01.pdf
strings-150319180934-conversion-gate01.pdf
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
String
StringString
String
 
week-7x
week-7xweek-7x
week-7x
 
Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
 
String
StringString
String
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functions
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
Arrays & Strings.pptx
Arrays & Strings.pptxArrays & Strings.pptx
Arrays & Strings.pptx
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdf
 
Program to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical orderProgram to sort the n names in an alphabetical order
Program to sort the n names in an alphabetical order
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
C Programming Strings.docx
C Programming Strings.docxC Programming Strings.docx
C Programming Strings.docx
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
 

More from Mohammad Imam Hossain

DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchMohammad Imam Hossain
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionMohammad Imam Hossain
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaMohammad Imam Hossain
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaMohammad Imam Hossain
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckMohammad Imam Hossain
 

More from Mohammad Imam Hossain (20)

DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6DS & Algo 6 - Offline Assignment 6
DS & Algo 6 - Offline Assignment 6
 
DS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic ProgrammingDS & Algo 6 - Dynamic Programming
DS & Algo 6 - Dynamic Programming
 
DS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MSTDS & Algo 5 - Disjoint Set and MST
DS & Algo 5 - Disjoint Set and MST
 
DS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path SearchDS & Algo 4 - Graph and Shortest Path Search
DS & Algo 4 - Graph and Shortest Path Search
 
DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3DS & Algo 3 - Offline Assignment 3
DS & Algo 3 - Offline Assignment 3
 
DS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and ConquerDS & Algo 3 - Divide and Conquer
DS & Algo 3 - Divide and Conquer
 
DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2DS & Algo 2 - Offline Assignment 2
DS & Algo 2 - Offline Assignment 2
 
DS & Algo 2 - Recursion
DS & Algo 2 - RecursionDS & Algo 2 - Recursion
DS & Algo 2 - Recursion
 
DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1DS & Algo 1 - Offline Assignment 1
DS & Algo 1 - Offline Assignment 1
 
DS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL IntroductionDS & Algo 1 - C++ and STL Introduction
DS & Algo 1 - C++ and STL Introduction
 
DBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMSDBMS 1 | Introduction to DBMS
DBMS 1 | Introduction to DBMS
 
DBMS 10 | Database Transactions
DBMS 10 | Database TransactionsDBMS 10 | Database Transactions
DBMS 10 | Database Transactions
 
DBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational SchemaDBMS 3 | ER Diagram to Relational Schema
DBMS 3 | ER Diagram to Relational Schema
 
DBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship ModelDBMS 2 | Entity Relationship Model
DBMS 2 | Entity Relationship Model
 
DBMS 7 | Relational Query Language
DBMS 7 | Relational Query LanguageDBMS 7 | Relational Query Language
DBMS 7 | Relational Query Language
 
DBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML CommandsDBMS 4 | MySQL - DDL & DML Commands
DBMS 4 | MySQL - DDL & DML Commands
 
DBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR SchemaDBMS 5 | MySQL Practice List - HR Schema
DBMS 5 | MySQL Practice List - HR Schema
 
TOC 10 | Turing Machine
TOC 10 | Turing MachineTOC 10 | Turing Machine
TOC 10 | Turing Machine
 
TOC 9 | Pushdown Automata
TOC 9 | Pushdown AutomataTOC 9 | Pushdown Automata
TOC 9 | Pushdown Automata
 
TOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity CheckTOC 8 | Derivation, Parse Tree & Ambiguity Check
TOC 8 | Derivation, Parse Tree & Ambiguity Check
 

Recently uploaded

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 

Recently uploaded (20)

DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 

SPL 13 | Character Array(String) in C

  • 1. Structured Programming Language Strings in C Mohammad Imam Hossain, Lecturer, CSE, UIU
  • 2. String • A String is an array of characters terminated by a special symbol named as null character (represented as ‘0’ ) • Null character is also named as string- termination character and used to identify the length of the string. • Logical illustration of a string consisting of n characters: X[0] X[1] X[2] … … … X[n-1] ‘0’
  • 3. Defining a String char string_name[expression]; • string_name  user chosen name for the string • expression integer expression indicates the number of elements in the string, including the null character Sample Code: char name[50];
  • 4. String vs Char Array Operations string char array declaration char name[50]; ///max 49 characters char name[50]; ///max 50 characters initialization char name[50]=“SPL"; or, char name[50]={'S','P','L','0'}; or, char name[50]={'S','P','L'}; char name[50]={'S','P','L'};
  • 5. Reading a String string char array char str[50]; scanf("%s", str); Or, gets(str); Or, scanf("%[^n]", str); char str[50]; int sz; printf("Enter the size: "); scanf("%d",&sz); int i; for(i=0;i<sz;i++){ scanf("%c",&str[i]); }
  • 6. Writing a String string char array char str[50]; printf("String : %sn",str); Or, puts(str); char str[50]; for(i=0;i<sz;i++){ printf("%c",str[i]); } printf("n");
  • 7. String I/O character wise char str[50]; ///reading string character wise int i=0; do{ scanf("%c",&str[i]); i=i+1; }while(str[i-1]!='n'); str[i-1]='0'; ///writing string character wise i=0; while(str[i]!='0'){ printf("%c",str[i]); i=i+1; } printf("n");
  • 8. String Processing • Way 1 using some predefined functions with help of <string.h> header file • Way 2 processing all characters individually
  • 9. String Length Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); int i=0; while(str[i]!='0'){ i++; } int length=i; printf("Length %dn",length); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); int length=strlen(str); printf("Length %dn",length); return 0; }
  • 10. String Concatenation Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char dest_string[50],src_string[30]; gets(dest_string); gets(src_string); printf("%sn",dest_string); int len1=strlen(dest_string); int i=0; while(src_string[i]!='0'){ dest_string[len1+i]=src_string[i]; i++; } dest_string[len1+i]='0'; printf("%sn",dest_string); return 0; } #include <stdio.h> #include <string.h> int main() { char dest_string[50],src_string[30]; gets(dest_string); gets(src_string); printf("%sn",dest_string); strcat(dest_string,src_string); printf("%sn",dest_string); return 0; }
  • 11. String Copy Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char dest_string[50]="hello...",src_string[30]; gets(src_string);///world int i=0; while(src_string[i]!='0'){ dest_string[i]=src_string[i]; i++; } dest_string[i]='0'; printf("%s",dest_string);///world return 0; } #include <stdio.h> #include <string.h> int main() { char dest_string[50]="hello…",src_string[30]; gets(src_string);///world strcpy(dest_string,src_string); printf("%s",dest_string);///world return 0; }
  • 12. String Compare Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str1[50],str2[50]; gets(str1); gets(str2); int i=0; while(str1[i]!='0' && str2[i]!='0'){ i++; } int cmp=str1[i]-str2[i]; if(cmp<0){ printf("str2 is greatern"); } else if(cmp>0){ printf("str1 is greatern"); } else if(cmp==0) { printf("Same stringn"); } return 0; } #include <stdio.h> #include <string.h> int main() { char str1[50],str2[50]; gets(str1); gets(str2); if(strcmp(str1,str2)<0){ printf("str2 is greatern"); } else if(strcmp(str1,str2)>0){ printf("str1 is greatern"); } else if(strcmp(str1,str2)==0) { printf("Same stringn"); } return 0; }
  • 13. LowerCase Conversion Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); int i=0; while(str[i]!='0'){ if(str[i]>='A' && str[i]<='Z'){ str[i]='a'+str[i]-'A'; } i++; } printf("%sn",str); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); strlwr(str); printf("%sn",str); return 0; }
  • 14. UpperCase Conversion Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); strupr(str); printf("%sn",str); return 0; }
  • 15. Reversing a String Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); int len=strlen(str); int first=0; int last=len-1; while(first<last){ int tmp=str[first]; str[first]=str[last]; str[last]=tmp; first++; last--; } printf("%sn",str); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("%sn",str); strrev(str); printf("%sn",str); return 0; }
  • 16. String to integer Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("string: %sn",str); int digit=0; int i=0; while(str[i]!='0'){ digit=digit*10+str[i]-'0'; i++; } printf("integer: %dn",digit); return 0; } #include <stdio.h> #include <string.h> int main() { char str[50]; gets(str); printf("string: %sn",str); int digit=atoi(str); printf("integer: %dn",digit); return 0; }
  • 17. Integer to string Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> void int_to_str(int val,char str[], int base) { int index=0; while(val!=0) { int rem=val%base; if(rem>9) { rem='a'+rem-10; str[index]=rem; } else str[index]='0'+rem; val=val/base; index++; } str[index]='0'; strrev(str); return ; } #include <stdio.h> #include <string.h> int main() { char str[50]; int num; scanf("%d",&num); printf("integer: %dn",num); itoa(num,str,10);///value, string name, base of conversion system printf("decimal string: %sn",str); itoa(num,str,2);///converting into binary printf("binary string: %sn",str); itoa(num,str,8);///converting into octal printf("octal string: %sn",str); itoa(num,str,16);///converting into hexadecimal printf("hexadecimal string: %sn",str); return 0; }
  • 18. Integer to string Character wise processing Header file <string.h> #include <stdio.h> #include <string.h> void int_to_str(int val,char str[], int base) { int index=0; while(val!=0) { int rem=val%base; if(rem>9) { rem='a'+rem-10; str[index]=rem; } else str[index]='0'+rem; val=val/base; index++; } str[index]='0'; strrev(str); return ; } #include <stdio.h> #include <string.h> int main() { char str[50]; int num; scanf("%d",&num); printf("integer: %dn",num); itoa(num,str,10);///value, string name, base of conversion system printf("decimal string: %sn",str); itoa(num,str,2);///converting into binary printf("binary string: %sn",str); itoa(num,str,8);///converting into octal printf("octal string: %sn",str); itoa(num,str,16);///converting into hexadecimal printf("hexadecimal string: %sn",str); return 0; } int main() { char str[50]; int num,base; printf("Enter number and base to convert: "); scanf("%d %d",&num,&base); printf("integer: %dn",num); int_to_str(num,str,base); printf("string: %s",str); return 0; }
  • 19. References • Programming with C, Schaum’s outline, 3rd edition, chapter 10 • https://www.tutorialspoint.com/cprogramming/c_str ings.htm