UNIT 4 C
 String: Introduction,
 predefined string functions,
 Manipulation of text data,
 Command Line Arguments.
• Strings are defined as an array of characters.
• The difference between a character array and a string is the string is
terminated with a special character ‘0’.
Declaration of strings:
• Declaring a string is as simple as declaring a one dimensional array.
• char str_name[size];
•
String predefined functions
Strlen
Strcat
Strcpy
a) strlen
• calculates the length of string
• strlen doesn't count '0' while calculating the length of a string.
b) Strcpy
Copies a string into another.
strcpy(s1, s2) copies the second string s2 to the first string s1.
C) Strcmp –
strcmp(s1, s2) compares two strings and finds out whether they are same or different.
It compares the two strings character by character till there is a mismatch.
If the two strings are identical, it returns a 0. If not, then it returns the difference between
the ASCII values of the first non-matching pair of characters.
d)
e)strlwr()
• The strlwr method is used to convert all the characters in a given string into
lowercase.
f) strupr()
• The strupr(string) function returns string characters in uppercase.
g) strstr()
The strstr() function returns pointer to the first occurrence of the matched string in the given string.
It is used to return substring from first match till the last character.
Syntax: char *strstr(const char *string, const char *match)
String programs without
using built-in function
/* C program to find the length of a string without using the built-in function */
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string n");
gets(string);
for (i = 0; string[i] != '0'; i++) /* keep going through each character of the string till its end */
{
length++;
}
printf("The length of a string is the number of characters in it n");
printf("So, the length of %s = %dn", string, length);
}
Program : Reverse String Without Using Library Function [ Strrev ]
#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
printf("nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("nReverse string is :%s", str);
return (0); }
Program : Copy One String into Other Without Using Library Function.
#include<stdio.h>
int main() {
char s1[100], s2[100];
int i;
printf("nEnter the string :");
gets(s1);
i = 0;
while (s1[i] != '0’)
{ s2[i] = s1[i];
i++;
}
s2[i] = '0';
printf("nCopied String is %s ", s2);
return (0);
}
//lower to upper case without using string function
int main()
{ char str[10];
int i=0;
printf("enter string");
scanf("%s",str);
while(str[i]!='0')
{
str[i]=str[i]-32; // change in case of upper to lower
i++;
}
printf("upper string is %s",str);
getch();
return 0;
}
//program to concatenate two strings
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
int main()
{
char s1[50], s2[30];
printf("nEnter String 1 :");
gets(s1);
printf("nEnter String 2 :");
gets(s2);
concat(s1, s2);
printf("nConcated string is :%s", s1);
return (0);
}
void concat(char s1[], char s2[])
{
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '0';
}
//program to find string is palindrome or not using string function
What is Palindrome Number ?
• a word, phrase, or sequence that
reads the same backwards as
forwards
• Some palindrome strings
examples are "dad", "radar",
"madam" etc.
How check given string is
palindrome number or not ?
•
This program works as follows :-
at first we copy the entered string
into a new string, and then we
reverse the new string and then
compares it with original string. If
both of them have same
sequence of characters i.e. they
are identical then the entered
string is a palindrome otherwise
not.
#include <stdio.h>
#include <conio.h>
void main() {
char *a;
int i,len,flag=0;
clrscr();
printf("nENTER A STRING: ");
gets(a);
len=strlen(a);
for (i=0;i<len;i++) {
if(a[i]==a[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("nTHE STRING IS PALINDROM"); else
printf("nTHE STRING IS NOT PALINDROM");
getch();
}
//program to find string is palindrome or not without using string function
What is Palindrome Number ?
• a word, phrase, or sequence that
reads the same backwards as
forwards
• Some palindrome strings
examples are "dad", "radar",
"madam" etc.
How check given string is
palindrome number or not ?
•
This program works as follows :-
at first we copy the entered string
into a new string, and then we
reverse the new string and then
compares it with original string. If
both of them have same
sequence of characters i.e. they
are identical then the entered
string is a palindrome otherwise
not.
#include <stdio.h>
#include <string.h>
int main() {
char text[100];
int begin, middle, end, length = 0;
gets(text);
while ( text[length] != '0' )
length++;
end = length - 1;
middle = length/2;
for ( begin = 0 ; begin < middle ; begin++ ) {
if ( text[begin] != text[end] ) {
printf("Not a palindrome.n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.n");
return 0;
}
Palindrome program for number
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
//To Delete all occurrences of Character from the String
Method Used
1.we have accepted one
string from the user and
character to be deleted
from the user.
2. Now we have passed
these two parameters to
function which will delete
all occurrences of given
character from the string.
3. Whole String will be
displayed as output
except given Character.
#include<string.h>
void del(char str[], char ch);
void main()
{ char str[10];
char ch;
printf("nEnter the string : ");
gets(str);
printf("nEnter character which you want to delete : ");
scanf("%ch", &ch);
del(str, ch);
getch();
}
void del(char str[], char ch)
{ int i, j = 0;
int size;
char ch1;
char str1[10];
size = strlen(str);
for (i = 0; i < size; i++)
{ if (str[i] != ch)
{ ch1 = str[i];
str1[j] = ch1;
j++;
}
}
str1[j] = '0';
printf("ncorrected string is : %s", str1);
}
Reverse a string using pointers
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}
Command line arguments
• The arguments passed from command line are called command line arguments. These arguments
are handled by main() function.
• To support command line argument, you need to change the structure of main() function as given
below.
int main(int argc, char *argv[] )
Here, argc is an integer value that specifies number of command line arguments passed into the
program, including the name of the program.
The argv[] contains the total number of arguments. The first argument is the file name always.
The array of character pointers, argv contains the list of all the arguments. argv[0] is the name of
the program or an empty string if the name is not available.
Argv[1] to argv[argc-1] specifies the command line argument.
#include <stdio.h>
void main(int argc, char *argv[] )
{
printf("Program name is: %sn", argv[0]);
if(argc < 2)
{
printf("No argument passed through command line.n");
}
else
{
printf("First argument is: %sn", argv[1]);
}
}

UNIT 4C-Strings.pptx for c language and basic knowledge

  • 1.
    UNIT 4 C String: Introduction,  predefined string functions,  Manipulation of text data,  Command Line Arguments.
  • 2.
    • Strings aredefined as an array of characters. • The difference between a character array and a string is the string is terminated with a special character ‘0’. Declaration of strings: • Declaring a string is as simple as declaring a one dimensional array. • char str_name[size]; •
  • 8.
  • 10.
    a) strlen • calculatesthe length of string • strlen doesn't count '0' while calculating the length of a string.
  • 11.
    b) Strcpy Copies astring into another. strcpy(s1, s2) copies the second string s2 to the first string s1.
  • 12.
    C) Strcmp – strcmp(s1,s2) compares two strings and finds out whether they are same or different. It compares the two strings character by character till there is a mismatch. If the two strings are identical, it returns a 0. If not, then it returns the difference between the ASCII values of the first non-matching pair of characters.
  • 13.
  • 15.
    e)strlwr() • The strlwrmethod is used to convert all the characters in a given string into lowercase.
  • 16.
    f) strupr() • Thestrupr(string) function returns string characters in uppercase.
  • 17.
    g) strstr() The strstr()function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character. Syntax: char *strstr(const char *string, const char *match)
  • 18.
  • 19.
    /* C programto find the length of a string without using the built-in function */ void main() { char string[50]; int i, length = 0; printf("Enter a string n"); gets(string); for (i = 0; string[i] != '0'; i++) /* keep going through each character of the string till its end */ { length++; } printf("The length of a string is the number of characters in it n"); printf("So, the length of %s = %dn", string, length); }
  • 20.
    Program : ReverseString Without Using Library Function [ Strrev ] #include<stdio.h> #include<string.h> int main() { char str[100], temp; int i, j = 0; printf("nEnter the string :"); gets(str); i = 0; j = strlen(str) - 1; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } printf("nReverse string is :%s", str); return (0); }
  • 23.
    Program : CopyOne String into Other Without Using Library Function. #include<stdio.h> int main() { char s1[100], s2[100]; int i; printf("nEnter the string :"); gets(s1); i = 0; while (s1[i] != '0’) { s2[i] = s1[i]; i++; } s2[i] = '0'; printf("nCopied String is %s ", s2); return (0); }
  • 24.
    //lower to uppercase without using string function int main() { char str[10]; int i=0; printf("enter string"); scanf("%s",str); while(str[i]!='0') { str[i]=str[i]-32; // change in case of upper to lower i++; } printf("upper string is %s",str); getch(); return 0; }
  • 25.
    //program to concatenatetwo strings #include<stdio.h> #include<string.h> void concat(char[], char[]); int main() { char s1[50], s2[30]; printf("nEnter String 1 :"); gets(s1); printf("nEnter String 2 :"); gets(s2); concat(s1, s2); printf("nConcated string is :%s", s1); return (0); } void concat(char s1[], char s2[]) { int i, j; i = strlen(s1); for (j = 0; s2[j] != '0'; i++, j++) { s1[i] = s2[j]; } s1[i] = '0'; }
  • 27.
    //program to findstring is palindrome or not using string function What is Palindrome Number ? • a word, phrase, or sequence that reads the same backwards as forwards • Some palindrome strings examples are "dad", "radar", "madam" etc. How check given string is palindrome number or not ? • This program works as follows :- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not. #include <stdio.h> #include <conio.h> void main() { char *a; int i,len,flag=0; clrscr(); printf("nENTER A STRING: "); gets(a); len=strlen(a); for (i=0;i<len;i++) { if(a[i]==a[len-i-1]) flag=flag+1; } if(flag==len) printf("nTHE STRING IS PALINDROM"); else printf("nTHE STRING IS NOT PALINDROM"); getch(); }
  • 28.
    //program to findstring is palindrome or not without using string function What is Palindrome Number ? • a word, phrase, or sequence that reads the same backwards as forwards • Some palindrome strings examples are "dad", "radar", "madam" etc. How check given string is palindrome number or not ? • This program works as follows :- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not. #include <stdio.h> #include <string.h> int main() { char text[100]; int begin, middle, end, length = 0; gets(text); while ( text[length] != '0' ) length++; end = length - 1; middle = length/2; for ( begin = 0 ; begin < middle ; begin++ ) { if ( text[begin] != text[end] ) { printf("Not a palindrome.n"); break; } end--; } if( begin == middle ) printf("Palindrome.n"); return 0; }
  • 29.
    Palindrome program fornumber #include<stdio.h> int main() { int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num) { r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp); return 0; }
  • 30.
    //To Delete alloccurrences of Character from the String Method Used 1.we have accepted one string from the user and character to be deleted from the user. 2. Now we have passed these two parameters to function which will delete all occurrences of given character from the string. 3. Whole String will be displayed as output except given Character. #include<string.h> void del(char str[], char ch); void main() { char str[10]; char ch; printf("nEnter the string : "); gets(str); printf("nEnter character which you want to delete : "); scanf("%ch", &ch); del(str, ch); getch(); } void del(char str[], char ch) { int i, j = 0; int size; char ch1; char str1[10]; size = strlen(str); for (i = 0; i < size; i++) { if (str[i] != ch) { ch1 = str[i]; str1[j] = ch1; j++; } } str1[j] = '0'; printf("ncorrected string is : %s", str1); }
  • 31.
    Reverse a stringusing pointers #include <stdio.h> #include <conio.h> void main() { char *s; int len,i; clrscr(); printf("nENTER A STRING: "); gets(s); len=strlen(s); printf("nTHE REVERSE OF THE STRING IS:"); for(i=len;i>=0;i--) printf("%c",*(s+i)); getch(); }
  • 32.
  • 33.
    • The argumentspassed from command line are called command line arguments. These arguments are handled by main() function. • To support command line argument, you need to change the structure of main() function as given below. int main(int argc, char *argv[] ) Here, argc is an integer value that specifies number of command line arguments passed into the program, including the name of the program. The argv[] contains the total number of arguments. The first argument is the file name always. The array of character pointers, argv contains the list of all the arguments. argv[0] is the name of the program or an empty string if the name is not available. Argv[1] to argv[argc-1] specifies the command line argument.
  • 34.
    #include <stdio.h> void main(intargc, char *argv[] ) { printf("Program name is: %sn", argv[0]); if(argc < 2) { printf("No argument passed through command line.n"); } else { printf("First argument is: %sn", argv[1]); } }