0% found this document useful (0 votes)
5 views

Lecture 4 Strings

Uploaded by

Abu Sayeed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Lecture 4 Strings

Uploaded by

Abu Sayeed
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Strings CSE

-271

Course Title: Introduction to Computer Programming

Presented By

Saadman Sakib

Lecturer,
Department of CSE,
CUET
Why do we need a terminating null character („\0‟)?

o A string is not a data type in C


o Considered as a data structure stored in an array
o The string is a variable-length structure and is stored in a
fixed-length array
o The array size is not always the size of the string
o Most often it is much larger than the string stored in it
o Therefore, the last element of the array need not represent the
end of the string
The null character serves as the “end-of-string” maker
#include <stdio.h>
int main( ) Use of getchar function
{
char a[100],ch;
int i=0;
printf("Enter the line of text\n");
do
{
ch=getchar( );
a[i]=ch;
i++;
}while(ch!='\n');
i=i-1;
a[i]='\0';
printf("%s",a);
return 0; }
C Program that will take a string as input and will reverse
the characteristics

#include <string.h>
#include <stdio.h>
int main()
{
char a[50],b[50];
int i,j,len;
gets(a);
len=strlen(a);
for(i=0,j=len-1;i<len;i++,j--)
b[j]=a[i];
for(i=0;i<len;i++)
putchar(b[i]);
}
#include <stdio.h>
Program-I
int main( )
{
int c,d;
char a[ ] = "CProgramming";
for(c=0;c<=11;c++)
{
d=c+1;
printf("%-12.*s\n",d,a);
}
for(c=11;c>=0;c--)
{
d=c+1;
printf("%-12.*s\n",d,a);
}
return 0;
}
#include <stdio.h>
int main( ) Program-II
{
int c,d;
char a[ ] = "CProgramming";
for(c=0;c<=11;c++)
{
d=c+1;
printf("%12.*s\n",d,a);
}
for(c=11;c>=0;c--)
{
d=c+1;
printf("%12.*s\n",d,a);
}
return 0;
}
C program that reads a string from the keyboard and
determines whether the string is a palindrome or not

#include <stdio.h> while(left<right &&


a=='t')
#include <string.h> {
main() if(b[left]==b[right]);
{ else
char b[50]; a='f';
left ++;
int len,left,right;
right --;
while(scanf("%s",&b)!=EOF) }
{ if(a =='t')
char a='t'; printf("Palindrome\n");
len=strlen(b); else
printf("Not
left=0; Palindrome\n");
right=len-1; }
return 0;
}
A C program to count vowels and consonants in a string using pointer

#include <stdio.h> case 'A':


#include <string.h> case 'E':
int main( ) case 'I':
{ case 'O':
char a[100]; case 'U':
char *s = a; v++;
int v=0,c=0; break;
printf("Enter the string:\n"); default:
gets(a); c++;
while(*s) }
{ }
if((*s>='a' && *s<='z') || (*s>='A' && *s<='Z')) s++;
{ }
switch(*s) printf("Total number of vowel = %d\n",v);
{ printf("Total number of consonant = %d\n",c);
case 'a': return 0;
case 'e': }
case 'i':
case 'o':
case 'u':
for(i=0;i<=n;i++)
Sorting of strings in alphabetical order
for(j=i+1;j<=n;j++)
{
#include <stdio.h> if(strcmp(a[i],a[j])>0
#include <string.h> )
int main( ) {
{ strcpy(tmp,a[i]);
int i,j,n; strcpy(a[i],a[j]);
char strcpy(a[j],tmp);
a[50][50],tmp[50];
}
scanf("%d",&n);
for(i=0;i<=n;i++) }
printf(“\nOutput:\n");
gets(a[i]);
for(i=0;i<=n;i++)
puts(a[i]);
return 0; }

You might also like