0% found this document useful (0 votes)
45 views32 pages

Computer Programming For Engineers: NAOE 2214

The document discusses string operations in C programming, including how to declare, initialize, take input and output of strings using functions like scanf, printf, gets, puts, and it provides an example code to find the frequency of a character in a given string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views32 pages

Computer Programming For Engineers: NAOE 2214

The document discusses string operations in C programming, including how to declare, initialize, take input and output of strings using functions like scanf, printf, gets, puts, and it provides an example code to find the frequency of a character in a given string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

NAOE 2214

Computer Programming for


Engineers

Md. Tusher Ahmed


Assistant Professor, Dept. of ME,BUET

02/06/21 1
Topic……

STRING

02/06/21
STRING OPERATIONS

• A String is a one-dimensional array of characters terminated by NULL character ‘\0’.

• Examples:
“Mechanical Engineering” “Bangladesh” ‘University” etc.

B A N G L A D E S H \0

Double quotes “Bangladesh”


Extra byte of memory
** A string is terminated by the NULL character and hence requires extra byte of memory (since
char type variable).
** String is always inside double quotation.

02/06/21 3
STRING OPERATIONS

Declaration
char stringname[size]; This is actually a char
char *stringname = “dhaka”; type array.
Example
char city[6]= “dhaka” ;
Or, char city[6]={‘d’,’h’,’a’,’k’,’a’};
Be careful about declaring the string. Just
like arrays these also give erroneous
results if memory limit is exceeded!
Remember!! This string
holds up to 5 characters!

02/06/21 4
STRING OPERATIONS

Initialization

02/06/21 5
STRING OPERATIONS
M
Me
Mec
Mech
Mecha
Mechan
We have done this type of Mechani
Mechanic
problems using loops. Now Mechanica
Mechanical
print this pattern using _____________
string! Mechanical
Mechanica
Mechanic
Mechani
Mechan
Mecha
Mech
Mec
Me
M

02/06/21 6
STRING OPERATIONS
#include<stdio.h>
#include<stdlib.h>
void main()
{
int a,b;
char dept[]="Mechanical";
for(a=0;a<=9;a++)
{
b=a+1;
printf("%.*s\n",b,dept);
}
printf("\n______________________\n\n");
for(a=9;a>=0;a--)
{
b=a+1;
printf("%.*s\n",b,dept);
}
}

02/06/21 7
STRING OPERATIONS
Input and Output operations in string…………..
For input and output operations involving strings there
are some dedicated functions. Some common functions
are…

Input Output
scanf() printf()
gets() puts()
getchar() putchar()

02/06/21 8
STRING OPERATIONS
Input and Output operations in string…………..
Our most familiar input and output
scanf & printf functions. Difference comes only in the form
of format specifier. For strings we use %s as
the specifier.
BE CAREFUL! YOU DON’T NEED TO USE ‘&’. NAME OF STRING
VARIABLE DENOTES MEMORY ADDRESS.
#include<stdio.h>
void main()
{
char dept[25];
printf("Enter your Department
Name:\n");
scanf("%s",dept);
printf("%s",dept);
}

02/06/21 9
STRING OPERATIONS
Input and Output operations in string…………..
Limitation of scanf():
scanf & printf The scanf() function takes string portion
before a space. It rejects the latter part!

#include<stdio.h>
void main()
{
char dept[25];
printf("Enter your Department
Name:\n");
scanf("%s",dept);
printf("%s",dept);
}

02/06/21 10
STRING OPERATIONS
Input and Output operations in string…………..
To avoid problems of scanf(), a simple library
gets & puts function exists------> gets().
General format: gets(string name); Takes input until “Enter” is pressed.

puts(string name);
#include<stdio.h>
void main()
{
char dept[25];
printf("Enter your Department
Name:\n");
gets(dept);
puts(dept);
}

02/06/21 11
STRING OPERATIONS
Input and Output operations in string…………..

getchar & putchar


These two input and output functions are slightly different in
operation. These take inputs and outputs as one character at a
time. Consequently, these can be used for string operations using
loop. You will see, for this particular case, while loop seems a
better choice compared to for loop.

02/06/21 12
STRING OPERATIONS
Input and Output operations in string…………..

getchar & putchar


#include<stdio.h> This tells to take input until
void main() “Enter”. int getchar(void);
{ int putchar(int c); //Only character code is sent
char ch,a[50];
int i=0,n;
printf("Enter your Department Name:\n");
while((ch = getchar())!= '\n')
{
a[i] = ch;
i++;
}
a[i] = '\0'; Null termination.
for(n=0;a[n]!='\0';n++)
putchar(a[n]);
}

02/06/21 13
STRING OPERATIONS

Example 1: Write a simple code that checks all the


letters in your name and finds frequency of a
character. For example in “Mechanical” frequency of
‘M’ is 1.

02/06/21 14
STRING OPERATIONS
Example 1: Write a simple code that checks all the letters in your name and finds frequency of a character. For example
in “Mechanical” frequency of ‘M’ is 1.

#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;
}

02/06/21 15
STRING OPERATIONS

Exploring strings We can manipulate and do anything with strings using some
predefined library functions in C under a special header.
Header: <string.h> write #include <string.h>
Function What it does
strcpy() Copies data from string to string.
strlen() Counts the characters in string.
strcat() Adds one string to another.
strcmp() Compares strings.
strupr() Makes each letter uppercase.
strlwr() Makes each letter lowercase.
strrev() Reverses a string.
strdup() Duplicates a string.
strstr() Finds one string inside another.
strset() Replaces string with a character.

02/06/21 16
STRING OPERATIONS
General form:
strcpy()
strcpy(to,from)

#include <stdio.h>
#include<string.h>
void main()
{
char dept[80],a[80];
printf("Enter your Department name:\n");
gets(dept);
strcpy(a,dept);
printf(" \ndept:\"%s\" and\n
Copy : \"%s\"",dept,a);
}

02/06/21 17
STRING OPERATIONS
General form:
strlen()
strlen(string_name)
#include<stdio.h>
#include<string.h>
void main()
{
int n;
char dept[80];
printf("Enter your Department name:\n");
gets(dept);
n=strlen(dept);
printf(" \ncharacter length of \"%s\" is :
%d",dept,n);
}

02/06/21 18
STRING OPERATIONS
General form: concatenation = adding.
strcat()
strcat(to,from)
#include<stdio.h>
#include<string.h>
void main()
{
char dept[]="mechanical",a[]="
",b[12]="Engineering";
strcat(dept,a);
strcat(dept,b);
printf(" \ndept:\"%s\" ",dept);
}

02/06/21 19
STRING OPERATIONS
General form:
strupr()
strupr(string)
#include<stdio.h>
#include<string.h>
void main()
{
char dept[80];
printf("Enter your Department name:\n");
gets(dept);
strupr(dept);
printf(" \ndepartment of \"%s\" ",dept);
}

02/06/21 20
STRING OPERATIONS
General form:
strlwr()
strlwr(string)
#include<stdio.h>
#include<string.h>
void main()
{
char dept[80];
printf("Enter your Department name:\n");
gets(dept);
strlwr(dept);
printf(" \ndepartment of \"%s\" ",dept);
}

02/06/21 21
STRING OPERATIONS
General form: Returns 0 if two strings are same and returns
strcmp() difference of ASCII value of mismatched characters.
#include<stdio.h>
#include<string.h>
a = strcmp(string1, string2)
void main()
{
char a[50],b[30];
int n;
printf("Enter your first string:\n");
gets(a);
printf("Enter your second string:\n");
gets(b);
n=strcmp(a,b);
printf(" n= %d",n);
}

02/06/21 22
STRING OPERATIONS
General form:
strset()
strset(string,character)
#include<stdio.h>
#include<string.h>
void main()
{
char a[50],b;
printf("Enter your string:\n");
gets(a);
printf(" your string: %s \n",a);
printf("Enter your desired character:\n");
scanf("%c",&b);
strset(a,b);
printf(" your string: %s \n",a);
}

02/06/21 23
STRING OPERATIONS
General form:
strrev()
strrev(string)
#include<stdio.h>
#include<string.h>
void main()
{
char a[50];
printf("Enter your string:\n");
gets(a);
printf(" your string: %s \n",a);
strrev(a);
printf(" Reversed string: %s \n",a);
}

02/06/21 24
STRING OPERATIONS
General form:
strdup()
pointer2 = strdup(string1)
#include<stdio.h>
#include<string.h>
void main()
{
char a[50],*b;
int i;
printf("Enter your string1:\n");
gets(a);
printf(" your string: %s \n",a);
b = strdup(a);
printf(" Duplicate string: %s \n",b);
}

02/06/21 25
STRING OPERATIONS

String to number Header file:


stdlib.h

atoi(): Converts to int type data. Example: atoi(“2234”);

atof(): Converts to float type data.


atol(): Converts to long type data.
strtod(): Converts to double type data.

02/06/21 26
STRING OPERATIONS

Example
#include<stdio.h>
#include<stdlib.h>
void main()
{
char a[]="234.987";
int i;
double f;
i=atoi(a);
f=atof(a);
printf("string: %s \n",a);
printf("int: %d \n",i);
printf("float: %lf \n",f);
}

02/06/21 27
STRING OPERATIONS
Strings passed to function
#include<stdio.h>
#include<string.h>
void random(char *a);
void main()
{
char c[50];
printf("Enter A string:\n");
gets(c);
random(c);
}
void random(char *a)
{
int x;
x = strlen(a);
printf("\nNumber of characters is: %d",x);
}

02/06/21 28
STRING OPERATIONS
Array of strings!

Just like any other variable type, array of string can be


declared.
Method: char *str[5];

char *clubs[3] ={“Real”,”Barca”,”United”};

02/06/21 29
STRING OPERATIONS
Illustrative Example: Sort names of three clubs United, Barca, and Real in alphabetical order.
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void main()
{
int i;
char *temp,*nam[3] = {"United", "Real", "Barca"};
for
{
for(i=0; i<3; i++)
{
if(strcmp(nam[i],nam[i+1])>0)
{
temp = nam[i];
nam[i] = nam[i+1];
nam[i+1]= temp;
}
}
}
printf("\n\n\n********\n\n");
30
for(i=0; i<3; i++)
puts(nam[i]);
}
02/06/21
STRING OPERATIONS
ASSIGNMENTS!

Write a simple C program using strings that counts and prints the vowels in your
formal name.

02/06/21 31
Thank you

02/06/21 32

You might also like