SlideShare a Scribd company logo
Part 11
Rumman Ansari
string
C Programming String
1. array of character are called strings
2. The string in C programming language is actually a one-dimensional array of
characters
array of characters which is terminated by a null character '0'
Declaration and initialization
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
char greeting[] = "Hello";
Data_type string_name [ ];
char s[5];
Reading Strings from user.
char c[20];
scanf("%s",c);
Print Strings from user.
char name[20];
printf("%s",name);
#include <stdio.h>
int main ()
{
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'};
printf("Greeting message: %sn", greeting );
return 0;
}
Example
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;
}
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;
}
#include <stdio.h>
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;
}
There are predefined functions gets() and puts() in C language to read and
display string respectively.
S.N. Function & Purpose
1 strcpy(s1, s2);Copies string s2 into string s1.
2 strcat(s1, s2);Concatenates string s2 onto the end of string s1.
3 strlen(s1);Returns the length of string s1.
4 strcmp(s1, s2);Returns 0 if s1 and s2 are the same; less than 0 if s1<s2;
greater than 0 if s1>s2.
5 strchr(s1, ch);Returns a pointer to the first occurrence of character ch in
string s1.
6 strstr(s1, s2);Returns a pointer to the first occurrence of string s2 in string
s1.
strlwr() Converts string to lowercase
strupr() Converts string to uppercase
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
/* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %sn", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %sn", str1 );
/* total lenghth of str1 after concatenation */
len = strlen(str1);
printf("strlen(str1) : %dn", len );
return 0;
}
C Program to Find the Frequency of Characters in a String
#include <stdio.h>
int main(){
char c[1000],ch;
int i,count=0;
printf("Enter a string: ");
gets(c);
printf("Enter a character to find frequency: ");
scanf("%c",&ch);
for(i=0;c[i]!='0';++i)
{
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
C Program to Find the Number of Vowels, Consonants, Digits and White space
in a String
#include<stdio.h>
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
printf("Enter a line of string:n");
gets(line);
for(i=0;line[i]!='0';++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d",v);
printf("nConsonants: %d",c);
printf("nDigits: %d",d);
printf("nWhite spaces: %d",s);
return 0;
}
C Program to Remove all Characters in a String except alphabet
#include<stdio.h>
int main(){
char line[150];
int i,j;
printf("Enter a string: ");
gets(line);
for(i=0; line[i]!='0'; ++i)
{
while (!((line[i]>='a'&&line[i]<='z') || (line[i]>='A'&&line[i]<='Z' || line[i]=='0')))
{
for(j=i;line[j]!='0';++j)
{
line[j]=line[j+1];
}
line[j]='0';
}
}
printf("Output String: ");
puts(line);
return 0;
}

More Related Content

PPTX
C Programming Language Part 9
PPTX
C Programming Language Part 8
PPTX
C Programming Language Step by Step Part 5
PPTX
C Programming Language Part 7
PPTX
C Programming Language Part 4
PPTX
Intro to c chapter cover 1 4
PPTX
C Programming Language Part 6
PPTX
Decision making and branching
C Programming Language Part 9
C Programming Language Part 8
C Programming Language Step by Step Part 5
C Programming Language Part 7
C Programming Language Part 4
Intro to c chapter cover 1 4
C Programming Language Part 6
Decision making and branching

What's hot (20)

PPTX
Expressions using operator in c
PDF
7 functions
PPT
Functions and pointers_unit_4
PPTX
C Programming Language Step by Step Part 2
PDF
1 introducing c language
PPT
Lecture 6- Intorduction to C Programming
PDF
8 arrays and pointers
PPSX
C programming pointer
PPT
Mesics lecture 5 input – output in ‘c’
PDF
4 operators, expressions &amp; statements
PPT
Lecture 9- Control Structures 1
PPT
Lecture 8- Data Input and Output
PDF
C programming
PDF
9 character string &amp; string library
PPT
Input And Output
PPT
Lecture 18 - Pointers
PPTX
Basic Input and Output
PPTX
Input output functions
Expressions using operator in c
7 functions
Functions and pointers_unit_4
C Programming Language Step by Step Part 2
1 introducing c language
Lecture 6- Intorduction to C Programming
8 arrays and pointers
C programming pointer
Mesics lecture 5 input – output in ‘c’
4 operators, expressions &amp; statements
Lecture 9- Control Structures 1
Lecture 8- Data Input and Output
C programming
9 character string &amp; string library
Input And Output
Lecture 18 - Pointers
Basic Input and Output
Input output functions
Ad

Viewers also liked (20)

PPTX
C program to write c program without using main function
PPTX
C Programming Language Part 5
PPTX
C Programming Language Step by Step Part 1
PPTX
Basic c programming and explanation PPT1
PPTX
How c program execute in c program
PPTX
My first program in c, hello world !
PPTX
PPTX
C programming - String
PPT
Steps for Developing a 'C' program
PPSX
C programming string
PPTX
Programming in C Basics
PDF
Composicio Digital _Practica Pa4
PPTX
Introduction to Basic C programming 02
PPSX
C programming basics
PPTX
Function in c
PPT
Ch 15
PPT
Capital structure theories
PDF
Why Invest for Retirement?
DOCX
Convenzione cpsp apsp-dogane
PDF
"Charlotte's Web" marijuana supposed cure for kids' seizures but doctors skep...
C program to write c program without using main function
C Programming Language Part 5
C Programming Language Step by Step Part 1
Basic c programming and explanation PPT1
How c program execute in c program
My first program in c, hello world !
C programming - String
Steps for Developing a 'C' program
C programming string
Programming in C Basics
Composicio Digital _Practica Pa4
Introduction to Basic C programming 02
C programming basics
Function in c
Ch 15
Capital structure theories
Why Invest for Retirement?
Convenzione cpsp apsp-dogane
"Charlotte's Web" marijuana supposed cure for kids' seizures but doctors skep...
Ad

Similar to C Programming Language Part 11 (20)

PDF
Principals of Programming in CModule -5.pdfModule-4.pdf
PPT
Strings
DOCX
C Programming Strings.docx
PDF
[ITP - Lecture 17] Strings in C/C++
PPTX
String_C.pptx
PPT
14 strings
PPTX
programming for problem solving using C-STRINGSc
DOCX
string , pointer
DOC
String in c
PDF
Strings IN C
PPTX
Lecture_on_string_manipulation_functions.pptx
PPTX
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
PPTX
Module-2_Strings concepts in c programming
PDF
array, function, pointer, pattern matching
DOCX
Unitii string
PPSX
Strings
PPTX
introduction to strings in c programming
PPTX
Lecture 24 PART 1.pptxkhfwraetrsytfyugiuihjojiiyutdruot8
PDF
Data structure week 3
PPTX
Lecture 2. mte 407
Principals of Programming in CModule -5.pdfModule-4.pdf
Strings
C Programming Strings.docx
[ITP - Lecture 17] Strings in C/C++
String_C.pptx
14 strings
programming for problem solving using C-STRINGSc
string , pointer
String in c
Strings IN C
Lecture_on_string_manipulation_functions.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
Module-2_Strings concepts in c programming
array, function, pointer, pattern matching
Unitii string
Strings
introduction to strings in c programming
Lecture 24 PART 1.pptxkhfwraetrsytfyugiuihjojiiyutdruot8
Data structure week 3
Lecture 2. mte 407

More from Rumman Ansari (17)

PDF
Sql tutorial
PDF
C programming exercises and solutions
PDF
Java Tutorial best website
DOCX
Java Questions and Answers
DOCX
servlet programming
PPTX
Steps for c program execution
PPTX
Pointer in c program
PPTX
What is token c programming
PPTX
What is identifier c programming
PPTX
What is keyword in c programming
PPTX
Type casting in c programming
PPTX
C Programming Language Step by Step Part 3
DOCX
C Programming
PPTX
Tail recursion
PPTX
Tail Recursion in data structure
PDF
Spyware manual
PPTX
Linked list
Sql tutorial
C programming exercises and solutions
Java Tutorial best website
Java Questions and Answers
servlet programming
Steps for c program execution
Pointer in c program
What is token c programming
What is identifier c programming
What is keyword in c programming
Type casting in c programming
C Programming Language Step by Step Part 3
C Programming
Tail recursion
Tail Recursion in data structure
Spyware manual
Linked list

Recently uploaded (20)

PPTX
UNIT 4 Total Quality Management .pptx
PDF
composite construction of structures.pdf
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
web development for engineering and engineering
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PPT
Project quality management in manufacturing
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PDF
Automation-in-Manufacturing-Chapter-Introduction.pdf
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Welding lecture in detail for understanding
DOCX
573137875-Attendance-Management-System-original
PPTX
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
UNIT 4 Total Quality Management .pptx
composite construction of structures.pdf
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
web development for engineering and engineering
UNIT-1 - COAL BASED THERMAL POWER PLANTS
Embodied AI: Ushering in the Next Era of Intelligent Systems
Project quality management in manufacturing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Automation-in-Manufacturing-Chapter-Introduction.pdf
Operating System & Kernel Study Guide-1 - converted.pdf
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Foundation to blockchain - A guide to Blockchain Tech
Welding lecture in detail for understanding
573137875-Attendance-Management-System-original
Recipes for Real Time Voice AI WebRTC, SLMs and Open Source Software.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf

C Programming Language Part 11

  • 2. C Programming String 1. array of character are called strings 2. The string in C programming language is actually a one-dimensional array of characters array of characters which is terminated by a null character '0'
  • 3. Declaration and initialization char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; char greeting[] = "Hello"; Data_type string_name [ ]; char s[5];
  • 4. Reading Strings from user. char c[20]; scanf("%s",c); Print Strings from user. char name[20]; printf("%s",name);
  • 5. #include <stdio.h> int main () { char greeting[6] = {'H', 'e', 'l', 'l', 'o', '0'}; printf("Greeting message: %sn", greeting ); return 0; } Example
  • 6. 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; }
  • 7. 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; }
  • 8. #include <stdio.h> 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; } There are predefined functions gets() and puts() in C language to read and display string respectively.
  • 9. S.N. Function & Purpose 1 strcpy(s1, s2);Copies string s2 into string s1. 2 strcat(s1, s2);Concatenates string s2 onto the end of string s1. 3 strlen(s1);Returns the length of string s1. 4 strcmp(s1, s2);Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. 5 strchr(s1, ch);Returns a pointer to the first occurrence of character ch in string s1. 6 strstr(s1, s2);Returns a pointer to the first occurrence of string s2 in string s1. strlwr() Converts string to lowercase strupr() Converts string to uppercase
  • 10. #include <stdio.h> #include <string.h> int main () { char str1[12] = "Hello"; char str2[12] = "World"; char str3[12]; int len ; /* copy str1 into str3 */ strcpy(str3, str1); printf("strcpy( str3, str1) : %sn", str3 ); /* concatenates str1 and str2 */ strcat( str1, str2); printf("strcat( str1, str2): %sn", str1 ); /* total lenghth of str1 after concatenation */ len = strlen(str1); printf("strlen(str1) : %dn", len ); return 0; }
  • 11. C Program to Find the Frequency of Characters in a String #include <stdio.h> int main(){ char c[1000],ch; int i,count=0; printf("Enter a string: "); gets(c); printf("Enter a character to find frequency: "); scanf("%c",&ch); for(i=0;c[i]!='0';++i) { if(ch==c[i]) ++count; } printf("Frequency of %c = %d", ch, count); return 0; }
  • 12. C Program to Find the Number of Vowels, Consonants, Digits and White space in a String #include<stdio.h> int main(){ char line[150]; int i,v,c,ch,d,s,o; o=v=c=ch=d=s=0; printf("Enter a line of string:n"); gets(line); for(i=0;line[i]!='0';++i) { if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U') ++v; else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) ++c; else if(line[i]>='0'&&c<='9') ++d; else if (line[i]==' ') ++s; } printf("Vowels: %d",v); printf("nConsonants: %d",c); printf("nDigits: %d",d); printf("nWhite spaces: %d",s); return 0; }
  • 13. C Program to Remove all Characters in a String except alphabet #include<stdio.h> int main(){ char line[150]; int i,j; printf("Enter a string: "); gets(line); for(i=0; line[i]!='0'; ++i) { while (!((line[i]>='a'&&line[i]<='z') || (line[i]>='A'&&line[i]<='Z' || line[i]=='0'))) { for(j=i;line[j]!='0';++j) { line[j]=line[j+1]; } line[j]='0'; } } printf("Output String: "); puts(line); return 0; }