UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
1
Course : INTRODUCTION TO PROGRAMMING
TOPIC: Strings in C
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Strings in C
In C programming, the one-dimensional array of characters, which is terminated by a
null character '0‘ ,are called strings.
The following declaration and initialization create a string consisting of the word
"Hello". To hold the null character at the end of the array, the size of the
character array containing the string is one more than the number of characters
in the word "Hello."
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Strings in C
Actually, you do not place the null character at the end of a string constant.
The C compiler automatically places the '0' at the end of the string when it
initializes the array.
Memory presentation of the defined string is as below:
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Reading and writing Strings in C using scanf(), gets(), printf() and puts()
// C program to read strings
#include<stdio.h>
int main()
{
// declaring string
char str[50]; //or char *str;
// reading string
scanf("%s",str); //or
gets(str);
// print string
printf("%s",str); // or puts(str);
return 0;
}
You can see in the program that string can also
be read using a single scanf statement. Also you
might be thinking that why we have not used the
‘&’ sign with string name ‘str’ in scanf statement!
To understand this you will have to recall your
knowledge of scanf. We know that the ‘&’ sign is
used to provide the address of the variable to the
scanf() function to store the value read in
memory. As str [ ] is a character array so using
str without braces ‘[‘ and ‘]’ will give the base
address of this string.
That’s why we have not used ‘&’ in this case as
we are already providing the base address of the
string to scanf.
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Passing strings to function
We can pass strings to function in a same way we pass an array to
a function. Below is a sample program to do this:
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Build-in string functions
C supports a wide range of functions that manipulate null-terminated strings −
Sr.No. 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.
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Build-in string functions
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: WAP to count number of alphabets, digits, special
characters in a string
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Program to reverse a string
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Program to find whether the given string is a palindrome or not (with and w/o
using function)
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Program to count number of words in a string
Output:
I am in second semester ECE2
Total number of words = 6
Input:
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Program to sort a list of strings into alphabetical order.
#include<stdio.h>
void main()
{ char *T;
int i,j,k;
char *ARRAY[5]={“SUNIL”,”ANIL”,”DILIP”,”JAY”,”BHARAT”};
for(I=0;I<5;I++)
{ printf(“%s t”,ARRAY[I]); }
printf(“n”);
for(I=0;I<4;I++)
{
for(J=0;J<4-I;J++)
{
K=strcmp(ARRAY[J],ARRAY[J+1]);
if(K>0)
{
T=ARRAY[J];
ARRAY[J]=ARRAY[J+1];
ARRAY[J+1]=T;
}
}
}
for(I=0;I<5;I++)
{
printf(“%s t”,ARRAY[I]);
}
}
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Example: Program to reverse a string using recursion
Explanation: Recursive function (reverse) takes string pointer (str) as input and
calls itself with next location to passed pointer (str+1). Recursion continues
this way, when pointer reaches ‘0’, all functions accumulated in stack print
char at passed location (str) and return one by one.
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
Find the output
#include stdio.h>
main()
{
int i,j;
char *str=“CALIFORNIA”;
for(i=0;str[i];i++)
{
for(j=0;j<i;j++)
printf(“%c”,str[j]);
printf(“n”);
}
}
UNIT-IV Strings W3schools.in, Tutorialspoint.com
Geeksforgeeks.org, codeforwin.org
1. WAP to sort a set of names in alphatical order.
2. WAP to replaces a substring with another substring
3. WAP to count number of alphabets, digits, special characters
in a string

12 Strings.pptx c++ data structure string .

  • 1.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org 1 Course : INTRODUCTION TO PROGRAMMING TOPIC: Strings in C
  • 2.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Strings in C In C programming, the one-dimensional array of characters, which is terminated by a null character '0‘ ,are called strings. The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."
  • 3.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Strings in C Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '0' at the end of the string when it initializes the array. Memory presentation of the defined string is as below:
  • 4.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Reading and writing Strings in C using scanf(), gets(), printf() and puts() // C program to read strings #include<stdio.h> int main() { // declaring string char str[50]; //or char *str; // reading string scanf("%s",str); //or gets(str); // print string printf("%s",str); // or puts(str); return 0; } You can see in the program that string can also be read using a single scanf statement. Also you might be thinking that why we have not used the ‘&’ sign with string name ‘str’ in scanf statement! To understand this you will have to recall your knowledge of scanf. We know that the ‘&’ sign is used to provide the address of the variable to the scanf() function to store the value read in memory. As str [ ] is a character array so using str without braces ‘[‘ and ‘]’ will give the base address of this string. That’s why we have not used ‘&’ in this case as we are already providing the base address of the string to scanf.
  • 5.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Passing strings to function We can pass strings to function in a same way we pass an array to a function. Below is a sample program to do this:
  • 6.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Build-in string functions C supports a wide range of functions that manipulate null-terminated strings − Sr.No. 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.
  • 7.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Build-in string functions
  • 8.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: WAP to count number of alphabets, digits, special characters in a string
  • 9.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Program to reverse a string
  • 10.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Program to find whether the given string is a palindrome or not (with and w/o using function)
  • 11.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Program to count number of words in a string Output: I am in second semester ECE2 Total number of words = 6 Input:
  • 12.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Program to sort a list of strings into alphabetical order. #include<stdio.h> void main() { char *T; int i,j,k; char *ARRAY[5]={“SUNIL”,”ANIL”,”DILIP”,”JAY”,”BHARAT”}; for(I=0;I<5;I++) { printf(“%s t”,ARRAY[I]); } printf(“n”); for(I=0;I<4;I++) { for(J=0;J<4-I;J++) { K=strcmp(ARRAY[J],ARRAY[J+1]); if(K>0) { T=ARRAY[J]; ARRAY[J]=ARRAY[J+1]; ARRAY[J+1]=T; } } } for(I=0;I<5;I++) { printf(“%s t”,ARRAY[I]); } }
  • 13.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Example: Program to reverse a string using recursion Explanation: Recursive function (reverse) takes string pointer (str) as input and calls itself with next location to passed pointer (str+1). Recursion continues this way, when pointer reaches ‘0’, all functions accumulated in stack print char at passed location (str) and return one by one.
  • 14.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org Find the output #include stdio.h> main() { int i,j; char *str=“CALIFORNIA”; for(i=0;str[i];i++) { for(j=0;j<i;j++) printf(“%c”,str[j]); printf(“n”); } }
  • 15.
    UNIT-IV Strings W3schools.in,Tutorialspoint.com Geeksforgeeks.org, codeforwin.org 1. WAP to sort a set of names in alphatical order. 2. WAP to replaces a substring with another substring 3. WAP to count number of alphabets, digits, special characters in a string