0% found this document useful (0 votes)
18 views

string

C STRING CONCEPTS

Uploaded by

principal.sbvrdc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

string

C STRING CONCEPTS

Uploaded by

principal.sbvrdc
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

UNIT-III

A string is a sequence of characters enclosed within the double quotes. A string may include
letters, digits and various special characters such as +,-,*,/ and %.
Example:
1. “cnu”
2. “ashwika”
3. “1+2=3”
In C language a string is nothing but a null-terminated character array. This means that after
the last character, a null character (‘\0’) is stored to signify the end of the character array.
Example:
char str[]=”hello”;
Declaring and initializing string variables:
A string variable is declared as an array. The general form is:
Syntax: char string_name[size];
Example: char name[10];
char greeting[] = "Hello";

Following is the memory presentation of the above defined string in C/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.

READING STRINGS:
The string can be read from the user by using three ways
1. Using scanf() function.
2. Using gets() function.
3. Using getchar(),getch() and getche() functions repeatedly.

Scanf(): The scanf() function is used to read the string. We use the % s format specification
to read string of characters.
Example: char college[10];
scanf(“%s”,college);
No ampersand is required before the variable college.
While reading a value using this function there should not be any white space. The string is
terminated once the white space is encountered.
Gets(): This function is used to read characters until enter key is pressed.
Syntax: gets(variable_name);
Example: char city[10];
gets(city)

17
UNIT-III

Getchar(): This function is used to read a single character at a time. This reading is
terminated when new line character (‘\n’) is encountered.
Example: getchar(ck);

WRITING STRINGS
The string can be displayed on the screen using three ways.
1. Using printf() function.
2. Using puts() function.
3. Using putchar() function.
Printf(): This function is used to print the string on the screen. We use %s format
specification.
Ex: printf(“%s”,city);

Puts(): The puts() functions is used to display the string on the screen.
Syntax: puts(variable_name);
Ex: puts(city)

Putchar(): This function is used to print or display one character at a time on the screen.
Example: putchar(city);

STRING OPERTIONS

1. Length: Displays the number of characters present in a string, excluding null


character.
Example: A program to find the length of a string.
#include<stdio.h>
#include<conio.h>
main()
{ Output:
char str[100];
Enter the String
int i=0;
clrscr(); cnu reddy
printf("\n Enter the String \n ");
gets(str);
while(str[i]!='\0')
{
i++;
}
printf(" the length of the string is %d ",i);
getch();
}

18
UNIT-III

2. Converting characters of a string into upper case: Converts given small characters
into uppercase.
Example: program to convert lower case to upper case
#include<stdio.h> Ascii values of A-Z is 65-90
#include<conio.h>
Ascii values of a-z is 97-122
main()
{
char str[40],str1[40];
int i=0,j=0;
clrscr();
printf("\n Enter the string upto 40 characters \n");
gets(str);
while(str[i]!='\0') Output:
{
if(str[i]>='a'&& str[i]<='z') Enter the string upto 40 characters
str1[j]=str[i]-32;
else ashwika reddy
str1[j]=str[i];
i++; the string converted into upppercase is =ASHWIKA
j++; REDDY
}
str1[j]='\0';
printf("\n the string converted into upppercase is =%s ",str1);
getch();
}
3. Converting characters of a string to lower case: Converts given upper case
characters into lower characters.
Example: program to convert uppercase to lowercase
#include<stdio.h>
#include<conio.h>
main()
{
char str[40],str1[40];
int i=0,j=0;
clrscr();
printf("\n Enter the string upto 40 characters \n");
gets(str);
while(str[i]!='\0') OUTPUT:
{
if(str[i]>='A'&& str[i]<='Z') Enter the string upto 40 characters
str1[j]=str[i]+32;
else ashwika reddy
str1[j]=str[i];
i++; the string converted into upppercase is
j++; =ASHWIKA REDDY
}
str1[j]='\0';
printf("\n the string converted into upppercase is =%s ",str1);
getch();
}

19
UNIT-III

4. Concatenation: If str1 and str2 are two strings, then on concatenation str3 produces a
string which contains characters of str1 followed by the characters of str2.
Example: Concatenating two strings
#include<stdio.h>
Output:
#include<conio.h>
main() Enter the string one
{
char str[40],str1[40],str2[40]; sbvr degree
int i,j;
printf("\n Enter the string one \n"); Enter the string two
gets(str);
printf(" \n Enter the string two \n"); college
gets(str1);
for(i=0,j=0;str[i]!='\0';i++,j++) After concatenation the string is :sbvr degreecollege
str2[j]=str[i];
for(i=0;str1[i]!='\0';i++,j++)
str2[j]=str1[i];
str2[j]='\0';
printf("\n After concatenation the string is :");
printf("%s",str2);
getch();
}

5. Appending: Appending one string to another string involves copying the contents of
source string at the end of a destination string.
Example:
#include<stdio.h>
#include<conio.h>
Output:
main()
{ Enter the string1
char str[30],str1[30];
int i,j; i love
printf("\n Enter the string1 \n");
gets(str); Enter the string2
printf("\n Enter the string2 \n");
gets(str1); my mother
for(i=0;str[i]!='\0';i++);
for(j=0;str1[j]!='\0';j++,i++) The appended string is :i love my mother
{
str[i]=str1[j]; String2 is :my mother
}
str[i]='\0';
printf("\n The appended string is :%s",str);
printf("\n String2 is :");
printf("%s",str1);
getch();
}

20
UNIT-III

6. Comparing: comparing the strings.


Example:
#include<stdio.h>
#include<conio.h>
main()
{
char str1[30],str2[30];
int i,diff; Output
printf("\n Enter the first string : ");
gets(str1); Enter the first string : sbvr degree
printf("\n Enter the second string : "); college
gets(str2);
i=0;
while(str1[i]!='\0'|| str2[i]!='\0')
{ Enter the second string : SBVR
diff=str1[i]-str2[i]; degree college
if(diff!=0)
break;
i++; }
if(diff>0) String1 is biggest
printf(" \n String1 is biggest ");
else if(diff<0)
printf("\n String2 is biggest");
else
printf("\n Both the strings are equal ");
getch();
}

7. Reverse: Reverses The String


Example:
#include<stdio.h>
#include<conio.h> Output
main()
{ Enter the string
char str[30];
int j; sbvr degree college
printf("\n Enter the string \n");
gets(str); Reverse string
for(j=0;str[j]!='\0';j++);
egelloc eerged rvbs
printf(" \n Reverse string \n");
j=j-1;
while(j>=0)
{
printf("%c",str[j]);
j--;
}
getch();
}

21
UNIT-III

Write a program to sort the strings


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
char str[10][20], temp[20];
int i,j,n;
printf("\n Enter how many strings to sort : "); Output:
scanf("%d",&n); Enter how many strings to sort :
for(i=0;i<n;i++) 3
scanf("%s",str[i]); cnu
printf("\n before sorting \n"); reddy
for(i=0;i<n;i++)
printf("%s \n",str[i]); ashwika
for(i=0;i<n-1;i++)
{ before sorting
for(j=i+1;j<n;j++) cnu
{ reddy
if(strcmp(str[i],str[j])>0) ashwika
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]); After sorting
strcpy(str[j],temp); ashwika
} cnu
} reddy
}
printf("\n After sorting \n");
for(i=0;i<n;i++)
printf("%s \n",str[i]);
getch();
}

Write a program to check whether the string is palindrome or not


#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
Output:
{
char str[30]; Enter any string :malayalam
int i,j,n,flag=1;
printf(" \n Enter any string :"); The given string malayalam is palindrome
gets(str);
n=strlen(str);
for(i=0,j=n-1;i<n/2;i++,j--)
{
if(str[i]!=str[j])
{
flag=0;
break;

22
UNIT-III

}
}
if(flag==1)
printf("\n The given string %s is palindrome ",str);
else
printf(" \n The given string %s is not palindrome ",str);
getch();
}

CHARACTER MANIPULATION FUNCTIONS


Function Usage Example
isalnum(int c) Check whether character is isalpha(‘A’);
an alphanumeric character
isalpha(int c) Checks whether character c is isalpha(‘a’);
an alphabetic character
isdigit(int c) Checks whether character c is isdigit(3);
a digit
islower(int c) Checks whether the character islower(‘k’);
c is in lower case
isupper(int c) Chekcks whether the isupper(‘k’);
character c is in upper case
toupper(int c) Converts the character c to toupper(‘t’) returns T
upper case
tolower(int c) Converts the character c to tolower(‘T’) returns t
lower case

23

You might also like