[go: up one dir, main page]

0% found this document useful (0 votes)
17 views18 pages

SPL Lec 8 (String) - 1

The document discusses strings in C language. Strings can be defined as arrays of characters terminated by a null character. There are two ways to declare strings - using a character array or a string literal. Functions like strlen(), strcmp(), strcpy() and strrev() can be used to work with strings.

Uploaded by

saziaafrin143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views18 pages

SPL Lec 8 (String) - 1

The document discusses strings in C language. Strings can be defined as arrays of characters terminated by a null character. There are two ways to declare strings - using a character array or a string literal. Functions like strlen(), strcmp(), strcpy() and strrev() can be used to work with strings.

Uploaded by

saziaafrin143
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

String

The string can be defined as the one-dimensional array of characters terminated by a null ('\0').
Each character in the array occupies one byte of memory, and the last character must always
be 0. The termination character ('\0') is important in a string since it is the only way to identify
where the string ends.
There are two ways to declare a string in c language.
1. By char array
2. By string literal
Let's see the example of declaring string by char array in C language.

char ch[10]={‘s, 'a', ‘b', ‘b', ‘i', ‘r', ‘h', ‘o', ‘s', ‘s', '\0'};
Array Vs String
Example

#include<stdio.h>
#include <string.h>
int main(){
char ch[11]={‘S', 'a', ‘k', ‘i', ‘l','\0'};
char ch2[11]=“Sakil";
printf("Char Array Value is: %s\n", ch);
printf("String Literal Value is: %s\n", ch2);
return 0;
}
Reading a line of text
Example

#include<stdio.h>
#include <string.h>
int main(){
char name[50];
printf("Enter your name: ");
gets(name); //reads string from user
printf("Your name is: ");
puts(name); //displays string
return 0;
}
String pointer

String pointers are declared as a pointer to a char.


When there is a value assigned to the string pointer the NULL is
put at the end automatically.
Take a look at this example:
String pointer

It is not possible to read, with scanf(), a string with a string pointer. You have to use
a character array and a pointer. See this example:
String Function
Example of String using Function To Compare String

#include<stdio.h>
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
Example of String using Function To Find String Length

#include<stdio.h>
#include <string.h>
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}
Example of String using Function To Reverse String

#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
Math Function
Example of math functions found in math.h header file

#include<stdio.h>
#include <math.h>
int main(){
printf("\n%f",ceil(3.6));
printf("\n%f",ceil(3.3));
printf("\n%f",floor(3.6));
printf("\n%f",floor(3.2));
printf("\n%f",sqrt(16));
printf("\n%f",sqrt(7));
printf("\n%f",pow(2,4));
printf("\n%f",pow(3,3));
printf("\n%d",abs(-12));
return 0;
}
Lab Task:
Write a C program to Copy String Manually

You can use the strcpy( )


function to copy the content of
one string to another but, this
program copies the content of
one string to another manually
without using strcpy( ) function.
Write a C program to Find Number of Vowels, Consonants, Digits
and White Space Character
Write a C program to Reverse String

To solve this problem, two standard library


functions strlen() and strcpy() are used to
calculate length and to copy string
respectively.
Write a C program To Find Prime or Not Prime No
Write a C program to sort a string in alphabetic order

C program to sort a string in alphabetic order:


For example if user will enter a string
"programming" then output will be
"aggimmnoprr" or output string will contain
characters in alphabetical order.

You might also like