10/27/2023
Dr. Dileep Kumar Singh
Head, JLU-SOET
dileep.singh@jlu.edu.in
Problem Solving and Program
Design using C
Problem Solving And Program Design in C
UNIT –IV
B Tech/ B Tech (Hons.) CSE – 1st Sem.
Strings in C
Strings
• A sequence of characters is often referred to as a
character “string”.
• A string is stored in an array of type char ending
with the null character '\0 '.
• A string is declared as an array of characters
• char s[10]
Dr. Dileep Kumar Singh 1
10/27/2023
Strings
• A string containing a single character takes up 2 bytes
of storage.
Strings
Character vs. String
• A string constant is a sequence of characters
enclosed in double quotes.
– For example, the character string:
char s1[2]="a"; //Takes two bytes of
storage.
s1: a \0
– On the other hand, the character, in single quotes:
char s2= `a`; //Takes only one byte
s2: a
Dr. Dileep Kumar Singh 2
10/27/2023
C offers four main operations on strings
• strcpy - copy one string into another
• strcat - append one string onto the right side of the
other
• strcmp – compare alphabetic order of two strings
• strlen – return the length of a string
strcpy
• strcpy(destinationstring, sourcestring)
• Copies sourcestring into destinationstring
• For example
• strcpy(str, “hello world”); assigns “hello world” to the
string str
Example with strcpy
#include <stdio.h>
#include <string.h>
main()
{
char x[] = “Example with strcpy”;
char y[25];
printf(“The string in array x is %s \n “, x);
strcpy(y,x);
printf(“The string in array y is %s \n “, y);
}
Dr. Dileep Kumar Singh 3
10/27/2023
strcat
• strcat(destinationstring, sourcestring)
• appends sourcestring to right hand side of destinationstring
• For example if str had value “a big ”
• strcat(str, “hello world”); appends “hello world” to the string “a
big ” to get
• “ a big hello world”
10
Example with strcat
#include <stdio.h>
#include <string.h>
main()
{
char x[] = “Example with strcat”;
char y[]= “which stands for string concatenation”;
printf(“The string in array x is %s \n “, x);
strcat(x,y);
printf(“The string in array x is %s \n “, x);
11
strcmp
• strcmp(stringa, stringb)
• Compares stringa and stringb alphabetically
• Returns a negative value if stringa precedes stringb
alphabetically
• Returns a positive value if stringb precedes stringa
alphabetically
• Returns 0 if they are equal
• Note lowercase characters are greater than Uppercase
12
Dr. Dileep Kumar Singh 4
10/27/2023
13
Strings Comparison Example
str1 str2 return reason
value
“AAAA” “ABCD” <0 ‘A’ <‘B’
“B123” “A089” >0 ‘B’ > ‘A’
“127” “409” <0 ‘1’ < ‘4’
“abc888” “abc888” =0 equal string
“abc” “abcde” <0 str1 is a sub string of
str2
“3” “12345” >0 ‘3’ > ‘1’
14
Example with strcmp
#include <stdio.h>
#include <string.h>
main()
{
char x[] = “cat”;
char y[]= “cat”;
char z[]= “dog”;
if (strcmp(x,y) == 0)
printf(“The string in array x %s is equal to that in %s \n “,
x,y);
15
Dr. Dileep Kumar Singh 5
10/27/2023
continued
if (strcmp(x,z) != 0)
{printf(“The string in array x %s is not equal to that in z %s \n “,
x,z);
if (strcmp(x,z) < 0)
printf(“The string in array x %s precedes that in z %s \n “, x,z);
else
printf(“The string in array z %s precedes that in x %s \n “, z,x);
}
else
printf( “they are equal”);
}
16
strlen str = "tttt"
• strlen(str) returns length of string excluding null str= t t t t \0
character
• strlen(“tttt”) = 4 not 5 since \0 not counted
strlen("tttt")=4
17
Example with strlen
#include <stdio.h>
#include <string.h>
main()
{
int i, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if (x[i] == ‘t’) count++;
}
printf(“The number of t’s in %s is %d \n “, x,count);
18
Dr. Dileep Kumar Singh 6
10/27/2023
Vowels Example with strlen
#include <stdio.h>
#include <string.h>
main()
{
int i, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if ((x[i] == ‘a’)||(x[i]==‘e’)||(x[i]==‘I’)||(x[i]==‘o’)||(x[i]==‘u’)) count++;
}
printf(“The number of vowels’s in %s is %d \n “, x,count);
19
No of Words Example with strlen
#include <stdio.h>
#include <string.h>
main()
{
int i, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if ((x[i] == ‘ ‘) count++;
}
printf(“The number of words’s in %s is %d \n “, x,count+1);
20
No of Words Example with more than one space
between words
#include <stdio.h>
#include <string.h>
main()
{
int i,j, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if ((x[i] == ‘ ‘)
{ count++;
for(j=i;x[j] != ‘ ‘;j++);
i = j;
}
}
printf(“The number of words’s in %s is %d \n “, x,count+1);
21
Dr. Dileep Kumar Singh 7
10/27/2023
Input output functions of characters and strings
• getchar() reads a character from the screen in a non-
interactive environment
• getche() like getchar() except interactive
• putchar(int ch) outputs a character to screen
• gets(str) gets a string from the keyboard
• puts(str) outputs string to screen
22
Exercise 1
Output
1
12
123
1234
………….
1 2 3 4 5 6 7 8 9 10
23
Exercise 1
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
for(i=1;i <= j;i++)
{
printf(“%d “,i);
}
printf(“\n“);
}
}
24
Dr. Dileep Kumar Singh 8
10/27/2023
Exercise 2
Output
*
**
***
****
…………….
**********
25
Exercise 2
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
for(i=1;i <= j;i++)
{
printf(“* “);
}
printf(“\n“);
}
}
26
Exercise 3
• Output
***********
* *
* *
* *
* *
* *
* *
* *
* *
***********
27
Dr. Dileep Kumar Singh 9
10/27/2023
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
printf(“* “);
for(i=1;i <= 8;i++)
{
if ((j==1) || (j==10)) printf(“* “);
else
printf(“ “);
}
printf(“* \n “);
}
}
28
Some Useful C Character Functions
• Don't forget to #include <ctype.h> to get the function
prototypes.
29
Functions
• Function Return true if
• int isalpha(c); c is a letter.
• int isupper(c); c is an upper case
letter.
• int islower(c); c is a lower case letter.
• int isdigit(c); c is a digit [0-9].
30
Dr. Dileep Kumar Singh 10
10/27/2023
More Functions
• Function Return true if
• int isxdigit(c); c is a hexadecimal digit
[0-9A-Fa-f].
• int isalnum(c); c is an alphanumeric character (c
is a letter or a digit);
• int isspace(c); c is a SPACE, TAB, RETURN,
NEWLINE, FORMFEED, or vertical tab
character.
31
Even More C Functions
• Function Return true if
• int ispunct(c); c is a punctuation
character (neither control
nor alphanumeric).
• int isprint(c); c is a printing character.
• int iscntrl(c); c is a delete character
or ordinary control
character.
32
Still More C Functions
• Function Return true if
• int toupper(int c); convert character c to
upper case (leave it
alone if not lower)
• int tolower(int c); convert character c to
lower case (leave it
alone if not upper)
33
Dr. Dileep Kumar Singh 11
10/27/2023
Program to Reverse Strings
• Program to Reverse Strings
• #include <stdio.h>
#include <string.h>
int main ()
{
• int i;
char a[10];
char temp;
//clrscr(); // only works on windows
gets(a);
• for (i = 0; a[i] != '\0' ; i++);
• i--;
• for (int j = 0; j <= i/2 ; j++)
{
• temp = a[j];
a[j] = a[i - j];
a[i - j] = temp;
• }
printf("%s",a);
return(0);
•
}
34
Program to count the number of vowels in a string
:
• Note Two different ways to declare strings
• One using pointers *str
• Two using character array char a[]
• #include <stdio.h>
#include <string.h>
• void main() {
• char *str;
• char a[]="aeiouAEIOU";
• int i,j,count=0;
• clrscr();
• printf("\nEnter the string\n");
• gets(str);
• for(i=0;str[i]!='\0';i++)
• {
• for(j=0;a[j]!='\0';j++)
• if(a[j] == str[i]
• {
• count++;
• break;
•
}
printf("\nNo. of vowels = %d",count);
•
}
•
}
35
THANKS
36
Dr. Dileep Kumar Singh 12