Module 4 Strings Notes Python Vtu
Module 4 Strings Notes Python Vtu
Examples:
“HELLO WORLD”
“WELL DONE”
Declaration of a string
char s[5];
Initialization of Array:
char s[5]=“rama”;
The strings can be read from keyboard and can be displayed in 2 ways.
I/O
Functions
Formatted Unformatted
Reading Strings
o If we declare a string by writing
char str[100];
o We can read the string ‘str’ by using 3 ways, namely:
1. using scanf() function
2. using gets() function
3. using getchar(), getch() and getche() function repeatedly
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
OUTPUT:
Enter name: Dennis Ritchie
Your name is Dennis
Even though Dennis Ritchie was entered in the above program, only "Dennis" was
stored in the name string. It's because there was a space after Dennis.
Also notice that we have used the code name instead of &name with scanf().
This is because name is a char array, and we know that array names decay to
pointers in C.
Thus, the name in scanf() already points to the address of the first element in the
string, which is why we don't need to use &.
You entered:
Programming is the best
scanf() gets()
when scanf() is used to when gets() is used to read input it
read string input it stops stops reading input when it
reading when it encounters encounters newline or End Of File. It
whitespace, newline or End does not stop reading the input on
Of File encountering whitespace as it
considers whitespace as a string.
It is used to read input of It is used only for string input.
any datatype
Its syntax is -: Its syntax is -:
Writing Strings
Strings can be displayed on monitor screen using three ways:
1. Using printf() function
2. Using puts() function
3. Using putchar() function repeatedly
#include <stdio.h>
int main()
{
char name[20] = “Programming is Fun”;
printf("String content is \n%s.", name);
return 0;
}
Output:
String content is
Programming is Fun
The conversion character ‘s’ is used to print the string in the format specifier “%s”
The width and precision specifications also could be used along with format
specifier
o The width specifies the total number of characters to be displayed. If the string is
short, extra space is left padded or right padded
o A negative width justifies the string to the left rather its usual right justification
o The Precision specifies the maximum number of characters to be displayed.
o If the string is long, the extra characters are truncated
o If the field width is less than the length of the string, the entire string will be
printed
o If the precision is zero, then nothing is printed on the string
Example
#include <stdio.h>
int main()
{
char str[10] = "COMPUTER";
printf("|%s|\n",str);
printf("|%20s|\n",str);
printf("|%20.4s|\n",str);
printf("|%-20.4s|\n",str);
printf("|%5s|\n",str);
printf("|%20.0s|\n",str);
printf("|%*.*s|\n",10,5,str);
}
Output:
|COMPUTER|
| COMPUTER|
| COMP|
|COMP |
|COMPUTER|
| |
| COMPU|
Return Value
The return value of the puts function depends on the success/failure of its execution.
o On success, the puts() function returns a non-negative value.
o Otherwise, an End-Of-File (EOF) error is returned.
Example:
#include <stdio.h>
int main()
{
// using puts to print hello world
char str = "Programming in C";
puts(str);
puts("Programming is Fun");
return 0;
}
Output:
Programming in C
Programming is Fun
#include <stdio.h>
int main()
{
// using putchar to print Computer Device
char str = "Computer Device";
int i=0;
while(str[i]!=’\0’)
{
putchar(str[i]);
i++;
}
return 0;
}
Output:
Computer Device
Suppressing Input
The scanf() is used to read a data without assigning it to any variable.
This is done by preceeding that field’s format code with a ‘*’
The * is a suppressing character which tells scanf to consume the input but not
try to assign it to a variable and the c conversion specifier asks to read any character
Example:
#include <stdio.h>
int main()
{
scanf(“%d*c%d”,&hr,&min);
scanf("%s %*c %s",s1,s2);
printf("%s %s\n",s1,s2);
scanf("%d %*c %d",&hr,&min);
printf("%d %d\n",hr,min);
}
Output:
Hello - Everyone
Hello Everyone
10 : 45
10 45
The string can be read as Hello - Everyone, here ‘-‘ the, would be read but not
assigned to anything
Thus, assignment suppression is helpful when part of what input needs to be
suppressed
Using a Scanset
scanf family functions support scanset specifiers which are represented by %[].
Inside scanset, we can specify single character or range of characters. While
processing scanset, scanf will process only those characters which are part of scanset
We can define scanset by putting characters inside square brackets. Please note
that the scansets are case-sensitive.
Example:
scanf(%[A-Z_abc]s,str);
This will scan all the specified character in the scanset i.e., All capital letters, an
underscore and only lowercase a,b,c alphabets and stops reading the string on
encountering the first non scanset character from keyboard
If first character of scanset is ‘^’, then the specifier will stop reading after first
occurrence of that character. For example, given below scanset will read all
characters but stops after first occurrence of the character ‘9’
Example
scanf(%[^9]s,str);
C program to demonstrate scanset in scanf() function
#include <stdio.h>
int main(void)
{
char str[128],str2[20];
printf("Enter a string with : ");
scanf("%[ A-za-z0-9!@#]s", str1);
printf("You entered: %s\n", str);
printf("You entered: %s\n", str1);
printf("Enter a string without vowels: ");
scanf("%[^aeiou]s", str2);
printf("You entered: %s\n", str);
return 0;
}
Output:
Enter a string : Hello Everyone 123@#$%^&*ju
You entered: Hello Everyone 123@#
Enter a string without vowels : qwerty keyboard
You entered: qw
sprintf: It stands for "string print formatted." It is used to write formatted data to a
string.
Example:
#include <stdio.h>
int main() {
char buffer[50];
printf("%s\n", buffer);
return 0;
sscanf: It stands for "string scan formatted." It is used to read data from a string into
variables, using a format specifier.
#include <stdio.h>
int main() {
int num;
float fnum;
return 0;
In this example, sscanf reads the values of num and fnum from the string data using
the specified format, and then we print the extracted values.
In this example, sprintf formats the integer num and float fnum into the string
buffer, and then we print the contents of buffer which will be "The number is 42 and
the float is 3.14".
Operations on strings:
Step 3: SET I = I + 1
[END OF LOOP]
Step 5: END
Program:
#include <stdio.h>
int main()
char a[20],i=0,len;
gets(a);
while(a[i]!='\0')
i++;
len=i;
Output:
ELSE
[END OF IF]
SET I=I+1
[END OF LOOP]
Step 5: EXIT
Program:
#include <stdio.h>
int main()
char a[20],uprstr[20];
int i=0,j=0;
gets(a);
while(a[i]!='\0')
if(a[i]>='a'&&a[i]<='z')
uprstr[j]=a[i]-32;
else
uprstr[j]=a[i];
i++;
j++;
uprstr[j]='\0';
puts(uprstr);
Output:
ELSE
[END OF IF]
SET I=I+1
[END OF LOOP]
Step 5: EXIT
Program:
#include <stdio.h>
int main()
char a[20],lwrstr[20];
int i=0,j=0;
gets(a);
while(a[i]!='\0')
if(a[i]>='A'&&a[i]<='Z')
lwrstr[j]=a[i]+32;
else
lwrstr[j]=a[i];
i++;
j++;
lwrstr[j]='\0';
puts(lwrstr);
Output:
[END of loop]
[END of loop]
Step 6: EXIT
Program:
#include<stdio.h>
void main()
char a[20]="Hi,",b[20]="Nisha",c[20];
int i=0,j=0;
while(a[i]!='\0')
c[j]=a[i];
i++;
j++;
i=0;
while(b[i]!='\0')
c[j]=b[i];
i++;
j++;
c[j]='\0';
puts(c);
Output:
• Appending one string to another string involves copying the contents of the
source string at the end of the destination string.
• The appending operation would leave the source string S1 unchanged and
destination string S2 = S2+S1.
Step 3: SET I = I + 1
[END OF LOOP]
Step 6: SET I = I + 1
Step 7: SET J = J + 1
[END OF LOOP]
Step 9: EXIT
Program:
#include<stdio.h>
void main()
char dest[100],src[100];
int i=0,j=0;
gets(src);
gets(dest);
while(dest[i]!='\0')
i++;
while(src[j]!='\0')
dest[i]=src[j];
i++;
j++;
dest[i]='\0';
puts(dest);
Output:
6. Comparing 2 strings:
If S1 and S2 are two strings then comparing two strings will give either of these
results-
Algorithm:
ELSE
IF STR1[I] == STR2[I]
SET I = I + 1
ELSE
Go to Step 4
[END OF IF]
[END OF LOOP]
IF I = Len1, then
SET SAME =1
[END OF IF]
[END OF IF]
[END OF IF]
Step 5: EXIT
Program:
#include<stdio.h>
#include<string.h>
void main()
char a[100],b[100];
int i=0,len1=0,len2=0,same=0;
gets(a);
gets(b);
len1=strlen(a);
len2=strlen(b);
if(len1==len2)
while(i<len1)
if(a[i]==b[i])
i++;
else
break;
if(i==len1)
same=1;
if(len1!=len2)
if(same==0)
if(a[i]>b[i])
else if(a[i]<b[i])
Output:
7. Reversing a string:
• To reverse a string we just need to swap the first character with the last, second
character with the second last character, so on and so forth.
Step 4: SET I = I + 1, J = J – 1
[END OF LOOP]
Step 5: EXIT
Program:
#include <stdio.h>
#include <string.h>
int main ()
char a[100],temp;
int i=0,j=0;
gets(a);
j=strlen(a)-1;
while(i<j)
temp=a[j];
a[j]=a[i];
a[i]=temp;
i++;
j--;
puts(a);
Output:
• In order to extract a substring from the main string we need to copy the content of
the string starting from the first position to the nth position where n is the number of
characters to be extracted.
Step 4: SET I = I + 1
[END OF LOOP]
Step 6: EXIT
Program:
#include <stdio.h>
#include <string.h>
int main ()
char a[100],substr[100];
int i=0,n;
gets(a);
scanf("%d",&n);
i=0;
while(a[i]!='\0'&&i<n)
substr[i]=a[i];
i++;
substr[i]='\0';
puts(substr);
Output:
• In order to extract a substring from the right side of the main string we need to
first calculate the position. For example, if S1 = “Hello World” and we have to copy 7
characters starting from the right, then we have to actually start extracting
characters from the 5th position. This is calculated by, total number of characters – n
+ 1.
Step 4: SET I = I + 1, J = J + 1
[END OF LOOP]
Step 6: EXIT
Program:
#include <stdio.h>
#include <string.h>
int main ()
char a[100],substr[100];
int i=0,j=0,n;
gets(a);
scanf("%d",&n);
j=strlen(a)-n;
while(a[j]!='\0')
substr[i]=a[j];
i++;
j++;
substr[i]='\0';
puts(substr);
Output:
• To extract a substring from a given string requires information about three things
2. the position of the first character of the substring in the given string
SUBSTRING(str,15,5) = world
Step 4: SET I = I + 1
Step 5: SET J = J + 1
Step 6: SET N = N – 1
[END of loop]
Step 8: EXIT
Program:
#include <stdio.h>
#include <string.h>
int main ()
char a[100],substr[100];
int i=0,j=0,n,m;
gets(a);
scanf("%d",&m);
scanf("%d",&n);
i=m;
while(a[i]!='\0'&&n>0)
substr[j]=a[i];
i++; j++;n--;
substr[j]='\0';
puts(substr);
Output:
• The insertion operation inserts a string S in the main text, T at the kth position.
new_str[j] = str[k]
SET J=J+1
SET K = K+1
ELSE
new_str[[J] = text[I]
SET J = J+1
Step 6: EXIT
Program:
#include <stdio.h>
#include<string.h>
int main()
char str[100],subStr[50],newStr[100];
int n,i,j,k;
scanf("%s",str);
scanf("%s",subStr);
scanf("%d",&n);
for(i=0;i<strlen(str)-n;i++)
newStr[i] = str[i];
for(k=n,j=0;subStr[j]!='\0';k++)
newStr[k]=subStr[j++];
for(i=i;str[i]!='\0';i++)
newStr[k++] = str[i];
newStr[k]='\0';
return 0;
Output:
Enter a string:
Pram
ogr
Program
12. Indexing:
Algorithm to find the index of the first occurrence of a string within a given
text
Step 8: EXIT
SET I = I+1
SET N = N – 1
[END OF IF]
Step 5: SET J= J + 1
Step 6: SET I = I + 1
Step 8: EXIT
Program:
#include <stdio.h>
#include <string.h>
int main ()
char a[100],b[100],c[100];
int i=0,j=0,k,n=0,loop=0;
gets(a);
gets(b);
while(a[i]!='\0')
j=0,k=i;
while(a[k]==b[j]&&b[j]!='\0')
k++;
j++;
if(b[j]=='\0')
loop=k;
c[n]=a[loop];
i++;
loop++;
n++;
c[n]='\0';
puts(c);
Output:
uppercase
alphabet to
lowercase
alphabet.
13. toupper() This function Returns uppercase alphabet of the
converts lowercase corresponding lowercase alphabet
alphabet to
uppercase
alphabet.
Example:
#include<stdio.h>
#include<string.h>
void main()
char a[10];
int l;
scanf("%s",a);
l=strlen(a);
Output:
2. strcpy(dest str,src str): This function copies source string to destination string.
Example:
#include<stdio.h>
#include<string.h>
void main()
char a[10],b[10];
scanf("%s",a);
strcpy(b,a);
printf("\nValue of a: %s",a);
printf("\nValue of b: %s",b);
Output:
Value of a: Classroom
Value of b: Classroom
Example:
#include<stdio.h>
#include<string.h>
void main()
char a[10],b[10];
scanf("%s",a);
strncpy(b,a,3);
printf("\nValue of a: %s",a);
printf("\nValue of b: %s",b);
Output:
Value of a: Classroom
Value of b: Cla
Example:
#include<stdio.h>
#include<string.h>
void main()
char a[30],b[30];
int n;
scanf("%s",a);
scanf("%s",b);
n=strcmp(a,b);
if(n==0)
else
Output 1:
Output 2:
#include<stdio.h>
#include<string.h>
void main()
char a[30],b[30];
int n;
scanf("%s",a);
scanf("%s",b);
n=strncmp(a,b,3);
if(n==0)
else
Output:
#include<stdio.h>
#include<string.h>
void main()
char a[30],b[30];
scanf("%s",a);
scanf("%s",b);
strcat(a,b);
Output:
#include<stdio.h>
#include<string.h>
void main()
char a[30],b[30];
scanf("%s",a);
scanf("%s",b);
strncat(a,b,3);
Output:
#include <stdio.h>
#include <string.h>
int main ()
char *p;
p = strchr(str, ch);
return 0;
Output:
#include <stdio.h>
#include <string.h>
int main ()
p = strrchr(str, ch);
p2 = strrchr(str, 'i');
return 0;
Output:
#include <stdio.h>
#include <string.h>
int main () {
char *result;
return 0;
Output:
#include <stdio.h>
#include <string.h>
void main ()
int len;
char str1[100];
char str2[100];
printf("Enter str1:");
gets(str1);
printf("Enter str2:");
gets(str2);
len = strspn(str1,str2);
Output:
Enter str1:Class
Enter str2:Class
#include <stdio.h>
#include <string.h>
void main ()
int len;
char str1[100];
char str2[100];
printf("Enter str1:");
gets(str1);
printf("Enter str2:");
gets(str2);
len = strcspn(str1,str2);
Output:
Enter str1:Class
Enter str2:Room
#include <stdio.h>
#include <string.h>
int main ()
char *result;
return(0);
Output:
#include <string.h>
#include <stdio.h>
int main()
printf("%s\n", tok);
return 0;
Output:
Arrays of Strings:
char array_name[row_size][col_size];
Row 1
Row 2
Row 3
Row 4
Row 5
#include<stdio.h>
void main()
char names[5][10];
int i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",names[i]);
for(i=0;i<n;i++)
puts(names[i]);
Output:
Nisha
Isha
#include <stdio.h>
int main ()
int choice;
printf("\n1.Find Length\n2.Concatenate\n3.Compare\n4.Exit\n");
switch (choice)
case 1:
scanf("%s", string1);
break;
case 2:
join_strings(string1, string2);
break;
case 3:
compare_strings(string1, string2);
break;
case 4:
return 0;
int len = 0, i;
len++;
return len;
int i, j;
i = find_length(string1);
string1[i] = string2[j];
string1[i] = '\0';
int i = 0;
i++;
else
printf("string1 = string2");
return 0 ;
Output1:
1.Find Length
2.Concatenate
3.Compare
4.Exit
Output2:
room
Output3:
room
#include <stdio.h>
#include <string.h>
int main()
int i, j, len,flag=1;
scanf("%s",Str);
j = 0;
len = strlen(Str);
RevStr[j++] = Str[i];
RevStr[j] = '\0';
for(i=0;i<len;i++)
if(Str[i] != RevStr[i])
flag=0;
if(flag == 1)
else
return 0;
Output1:
Output2:
*****End*****