Professional Documents
Culture Documents
Lab 7
Objective:
Strings in C language.
Theory:
The string in C programming language is actually a one-dimensional array of characters which is
terminated by a null character '\0'. Thus a null-terminated string contains the characters that
comprise the string followed by a null.
Declaration of strings
Strings are declared in C in similar manner as arrays. Only difference is that, strings are
of char type.
char s[5];
Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','\0'};
OR;
char c[5]={'a','b','c','d','\0'};
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN
#include <stdio.h>
int main()
{
char name[30], ch;
int i = 0;
printf("Enter name: ");
while(ch != '\n') // terminates if user hit enter
{
ch = getchar();
name[i] = ch;
i++;
}
name[i] = '\0'; // inserting null character at end
printf("Name: %s", name);
return 0;
}
To make life easier, there are predefined functions gets() and puts in C language to read and
display string respectively.
#include <stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name); //Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}
Output
Enter name: Tom Hanks
Name: Tom Hanks
Student’s tasks:
1. This program shows a two dimensional character array namely aa, of fixed length (5
rows and 25 columns) or say 5 single dimensional arrays, each of length 25.
void main(void)
{
clrscr();
int b;
char aa[5][25]={
{"Software Engineering"},
{"Department"},
{"Sindh Madressatul Islam"},
{"University"},
{"Karachi"}
};
for(int a=0;a<=4;a++)
{
b=0;
while(aa[a][b]!=0)
{
printf("%c",aa[a][b]);
b++;
}
printf("\n");
}
getch();
}
Ans 1:
OUTPUT:
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN
2. This program checks whether the string is palindrome or not. (A palindrome is a word,
phrase, number, or other sequence of characters which reads the same backward or
forward.)
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the string to check if it is a palindrome\n");
gets(a);
strcpy(b,a);
strrev(b);
if( strcmp(a,b) == 0 ) /*if function returns <0 then str1<str2, if function returns >0
getch();
}
Ans 2:
OUTPUT:
}
printf("Frequency of %c = %d", ch, frequency);
return 0;
}
Ans 3:
OUTPUT:
4. This program counts number of Vowels, Consonants, Digits and White spaces in string.
#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
vowels = consonants = digits = spaces = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++spaces;
}
}
printf("Vowels: %d",vowels);
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
return 0;}
Ans 4:
OUTPUT:
5. Write a program to copy the contents of one string into another in the reverse order.
Ans 5:
#include <stdio.h>
int main() {
int copied[10];
int i, count;
count = 9;
copied[count] = original[i];
count--;}
return 0;
}
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN
6. Write a program which takes names of five countries as input and prints them in
alphabetical order.
Ans 6:
#include<stdio.h>
#include<string.h>
int main(){
int i,j,count;
char str[25][25],temp[25];
scanf("%d",&count);
for(i=0;i<=count;i++)
gets(str[i]);
for(i=0;i<=count;i++)
for(j=i+1;j<=count;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);}
for(i=0;i<=count;i++)
puts(str[i]);
return 0;
OUTPUT:
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN