Thanks to visit codestin.com
Credit goes to www.slideshare.net

/*Introduction to
Strings*/
< C Programming >
{
}
...
Table of contents
Strings, More about Strings…,Pointers &
strings;
stren(), strcyp(), strcat(),
strcmp();
//Uk it
01
02
03
Introduction
Standard Library String Functions
Programs
Whoa!
/* WHAT ARE STRINGS?? */
}
Char name[]={‘H’,’A’,’E’,’S’,’L’,’E’,’R’,’0’};
01
The way a group of integers can be stored in an integer
array,similarly a group of characters can be stored in a
character array. A string is a 1-D array of characters
terminated by a ' 0 '. ‘0’ is called null character. ‘0’
and ’0’ are not same. ASCII value of ‘0’ is 0,whereas
ASCII value of ‘0’ is 48. C concedes the fact that you
would use strings very often and hence provides a shortcut
for initializing strings;
{
} ..
/*MORE about Strings!*/
M
Character Arrays: In C, a string is essentially an array of characters
terminated by a null character ('0'). This null character is used to mark
the end of the string. For example:
char myString[] = "Hello, World";
String Input: You can use functions like scanf or gets (not recommended due to security
issues) to input strings from the user. For example:
char name[50];
printf("Enter your name: ");
scanf("%s", name);
String Output: You can use functions like printf to display strings. For example:
char greeting[] = "Hello, World";
printf("%sn", greeting);
/*POINTERS and STRINGS!*/
Do you know Pointers and strings are fundamental concepts
in programming, especially in languages like C and C++.
● It’s a variable that holds the address memory of
another variable
● It allows you to indirectly access and manipulate
data.
● This is how you declare it: C int*ptr;
This declares a pointer ptr that can point to an
integer.
● A string is an array of characters, typically
terminated by a null character ('0').
● In C, strings are represented as arrays of
characters. For example:
char str[] = "Hello";
*
MORE ABOUT POINTERS…
In this case, str is an array of characters containing
the string "Hello".
Pointers and strings often go together in C, as strings
are typically manipulated using pointers. For instance,
you might use a pointer to iterate through the
characters of a string or to pass a string to a
function.
Remember that in C, strings are represented as arrays
of characters, and there are various string functions
available in the standard library (like strlen, strcpy,
strcmp, etc.) to perform operations on strings.
{
}
Standard Library Functions
It returns the
number of characters
in a string.Syntax :
int strlen (string
name)
It is for copying
source string into
destination string
•The length of the
destination string >=
source string.Syntax
: strcpy
(Destination string,
Source String);
strcat ()
It combines two
strings.
The length of the
destination string
must be > than the
source string.
strlen () strcpy() ...
The predefined functions which are designed to handle strings are available
in the library string.h.
Standard Library Functions
This function compares 2 strings.
•It returns the ASCII
difference of the first
two non – matching
characters in both
strcmp()
...
strrev()
The strrev() function is a
built-in function in C
and is defined in string.h header file.
The strrev() function is used
to reverse the given string.
PROGRAMS
/* Basic string program */
#include <stdio.h>
#include<conio.h>
Void main
{
int roll_no.;
char name[20],address[20],branch[20]
printf(“n Enter roll no.”);
scanf(“%d”, &roll_no.);
printf(“enter name”);
scanf(“%s”, name);
printf(“n student name is %s”, name);
getch();
}
PROGRAMS
/* strlen() program */
#include <string.h>
main (){
char a[30]=“Hello”;
int l;
l=strlen (a);
printf(“length of the string=%d”,l);
getch();
}
PROGRAMS
/* strcpy() program */
#include<string.h>
main(){
char a[50];
printf(“Enter a source string”);
scanf(“%s”,a);
printf(“Enter destination string”);
scanf(“%s”,b);
strcpy(b,a);
printf(“copied string=%s”,b);
getch();
}
PROGRAMS
/* strcat() program */
#include<string.h>
main(){
char a[50]=“Hello”;
char b[20]=“Good Morning”;
clrscr();
strcat(a,b);
printf(“concatenated string=%s”,a);
getch();
}
PROGRAMS
/* string using if else program */
#include<stdio.h>
#include<string.h>
int main(){
char a[50],b[50];
int d;
printf(“Enter 2 strings:”);
scanf(“%s %s”,a,b);
d=strcmp(a,b);
if(d==0){
printf(“%s is (alphabetically) equal to %S”,a,b);
}else if (d>0){
printf(“%s is (alphabetically) greater than %s”,a,b);
}else if (d<0){
printf(“%s is (alphabetically) less than %s”,a,b);
}
/*strcmp() program*/
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[]
= "abcd";
int result;
/* comparing strings str1 and str2 */
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %dn", result);
/* comparing strings str1 and str3 */
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %dn", result);
return 0;
}
PROGRAMS
#include<stdio.h>
void main ()
{
char *p = "hello javatpoint";
printf("String p: %sn",p);
char *q;
printf("copying the content of p into q
...n");
q = p;
printf("String q: %sn",q);
}
PROGRAMS
/*strrev() program*/
#include <stdio.h>
#include <string.h>
int main()
{
char str[50] = “C PROGRAMMING";
printf("The given string is =%sn",
str);
printf("After reversing string is
=%s", strrev(str));
return 0;
}
PROGRAMS
THANKYOU
{…
}…

introduction to strings in c programming

  • 1.
    /*Introduction to Strings*/ < CProgramming > { } ...
  • 2.
    Table of contents Strings,More about Strings…,Pointers & strings; stren(), strcyp(), strcat(), strcmp(); //Uk it 01 02 03 Introduction Standard Library String Functions Programs
  • 3.
    Whoa! /* WHAT ARESTRINGS?? */ }
  • 4.
    Char name[]={‘H’,’A’,’E’,’S’,’L’,’E’,’R’,’0’}; 01 The waya group of integers can be stored in an integer array,similarly a group of characters can be stored in a character array. A string is a 1-D array of characters terminated by a ' 0 '. ‘0’ is called null character. ‘0’ and ’0’ are not same. ASCII value of ‘0’ is 0,whereas ASCII value of ‘0’ is 48. C concedes the fact that you would use strings very often and hence provides a shortcut for initializing strings; { } ..
  • 5.
    /*MORE about Strings!*/ M CharacterArrays: In C, a string is essentially an array of characters terminated by a null character ('0'). This null character is used to mark the end of the string. For example: char myString[] = "Hello, World"; String Input: You can use functions like scanf or gets (not recommended due to security issues) to input strings from the user. For example: char name[50]; printf("Enter your name: "); scanf("%s", name); String Output: You can use functions like printf to display strings. For example: char greeting[] = "Hello, World"; printf("%sn", greeting);
  • 6.
    /*POINTERS and STRINGS!*/ Doyou know Pointers and strings are fundamental concepts in programming, especially in languages like C and C++. ● It’s a variable that holds the address memory of another variable ● It allows you to indirectly access and manipulate data. ● This is how you declare it: C int*ptr; This declares a pointer ptr that can point to an integer. ● A string is an array of characters, typically terminated by a null character ('0'). ● In C, strings are represented as arrays of characters. For example: char str[] = "Hello"; *
  • 7.
    MORE ABOUT POINTERS… Inthis case, str is an array of characters containing the string "Hello". Pointers and strings often go together in C, as strings are typically manipulated using pointers. For instance, you might use a pointer to iterate through the characters of a string or to pass a string to a function. Remember that in C, strings are represented as arrays of characters, and there are various string functions available in the standard library (like strlen, strcpy, strcmp, etc.) to perform operations on strings. { }
  • 8.
    Standard Library Functions Itreturns the number of characters in a string.Syntax : int strlen (string name) It is for copying source string into destination string •The length of the destination string >= source string.Syntax : strcpy (Destination string, Source String); strcat () It combines two strings. The length of the destination string must be > than the source string. strlen () strcpy() ... The predefined functions which are designed to handle strings are available in the library string.h.
  • 9.
    Standard Library Functions Thisfunction compares 2 strings. •It returns the ASCII difference of the first two non – matching characters in both strcmp() ... strrev() The strrev() function is a built-in function in C and is defined in string.h header file. The strrev() function is used to reverse the given string.
  • 10.
    PROGRAMS /* Basic stringprogram */ #include <stdio.h> #include<conio.h> Void main { int roll_no.; char name[20],address[20],branch[20] printf(“n Enter roll no.”); scanf(“%d”, &roll_no.); printf(“enter name”); scanf(“%s”, name); printf(“n student name is %s”, name); getch(); }
  • 11.
    PROGRAMS /* strlen() program*/ #include <string.h> main (){ char a[30]=“Hello”; int l; l=strlen (a); printf(“length of the string=%d”,l); getch(); }
  • 12.
    PROGRAMS /* strcpy() program*/ #include<string.h> main(){ char a[50]; printf(“Enter a source string”); scanf(“%s”,a); printf(“Enter destination string”); scanf(“%s”,b); strcpy(b,a); printf(“copied string=%s”,b); getch(); }
  • 13.
    PROGRAMS /* strcat() program*/ #include<string.h> main(){ char a[50]=“Hello”; char b[20]=“Good Morning”; clrscr(); strcat(a,b); printf(“concatenated string=%s”,a); getch(); }
  • 14.
    PROGRAMS /* string usingif else program */ #include<stdio.h> #include<string.h> int main(){ char a[50],b[50]; int d; printf(“Enter 2 strings:”); scanf(“%s %s”,a,b); d=strcmp(a,b); if(d==0){ printf(“%s is (alphabetically) equal to %S”,a,b); }else if (d>0){ printf(“%s is (alphabetically) greater than %s”,a,b); }else if (d<0){ printf(“%s is (alphabetically) less than %s”,a,b); }
  • 15.
    /*strcmp() program*/ #include <stdio.h> #include<string.h> int main() { char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd"; int result; /* comparing strings str1 and str2 */ result = strcmp(str1, str2); printf("strcmp(str1, str2) = %dn", result); /* comparing strings str1 and str3 */ result = strcmp(str1, str3); printf("strcmp(str1, str3) = %dn", result); return 0; } PROGRAMS
  • 16.
    #include<stdio.h> void main () { char*p = "hello javatpoint"; printf("String p: %sn",p); char *q; printf("copying the content of p into q ...n"); q = p; printf("String q: %sn",q); } PROGRAMS
  • 17.
    /*strrev() program*/ #include <stdio.h> #include<string.h> int main() { char str[50] = “C PROGRAMMING"; printf("The given string is =%sn", str); printf("After reversing string is =%s", strrev(str)); return 0; } PROGRAMS
  • 18.