You are on page 1of 8

Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

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

Reading Strings from user

Reading words from user.


char c[20];
scanf("%s",c);
String variable c can only take a word. It is because when white space is encountered,
the scanf()function terminates.

C program to illustrate how to read string from terminal.

#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

scanf("%s", name); /* Array name alone works as a base address of array so we


use name instead of &name here */
printf("Your name is %s.", name);
return 0;
}
Output

Enter name: Dennis Ritchie


Your name is Dennis.

C program to read line of text character by character.

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

C program to read line of text using gets() and puts()

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

Both programs have the same output below:


Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

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

then str1>str2(according to alphabetical ordering) */


printf("Entered string is a palindrome.\n");
else
printf("Entered string is not a palindrome.\n");

getch();
}
Ans 2:
OUTPUT:

3. This program finds the frequency of character in string.


#include <stdio.h>
int main()
{
char str[1000], ch;
int i, frequency = 0;
printf("Enter a string: ");
gets(str);
printf("Enter a character to find the frequency: ");
scanf("%c",&ch);
for(i = 0; str[i] != '\0'; ++i)
{
if(ch == str[i])
++frequency;
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

}
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 original[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

int copied[10];

int i, count;

count = 9;

for(i = 0; i < 10; i++) {

copied[count] = original[i];

count--;}

printf("original -> copied \n");

for(i = 0; i < 10; i++) {

printf(" %2d %2d\n", original[i], copied[i]);

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];

puts("How many strings u are going to enter?: ");

scanf("%d",&count);

puts("Enter Strings one by one: ");

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

printf("Order of Sorted Strings:");

for(i=0;i<=count;i++)

puts(str[i]);

return 0;

OUTPUT:
Lab Manual Programming Fundamentals Course Supervisor: SIR BASIT HASSAN

_________ __________________ _____________________


Date Lab Instructor Sign. Course Instructor Sign.

You might also like