MA 511: Computer Programming: Partha Sarathi Mandal
MA 511: Computer Programming: Partha Sarathi Mandal
Lecture 4:
http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html
/* writr a c program to find the length of the string using strlen() function */
#include<stdio.h>
#include<string.h>
main(){
char name[100];
int length;
printf("Enter the string");
scanf("%s", name);
length=strlen(name);
printf("\nNumber of characters in the string is=%d\n",length);
}
• scanf statement has a draw back it just terminates the statement as soon as it finds
a blank space, suppose if we type the string New York then only the string new will
be read and since there is a blank space after word “New” it will terminate the
string.
scanf: String reading
• char text[80];
• scanf(“ %*^\n+”, text);
/* writr a c program to find the length of the string using strlen() function */
#include<stdio.h>
#include<string.h>
main(){
char name[100];
int length;
String=“xyz”;
string1=string2;
• Are not valid.
• To copy the chars in one string to another string we
may do so on a character to character basis.
• char a = ‘x’, b = ‘3’, c = ‘#’, text*18+ = “guwahati”;
• Are valid
String operations (string.h)
• Length (number of characters in the string).
– length=strlen(name);
• Concatentation (adding two are more strings)
– strcpy(string1,”sri”);
strcpy(string2,”Bhagavan”);
Printf(“%s”,strcat(string1,string2);
• Comparing two strings.
– strcmp(string1,string2)
• Copy(copies one string over another)
• Exercise: Substring (Extract substring from a given
string)
Example
Read a string than replace each character with an equivalent encoded character
char line[80];
int i;
printf(“Type a line of text\n”);
scanf(“%*^\n+”, line);
for(i=0; line[i+ != ‘\o’; ++i){
)
if(((line[i+>=‘0’) && (line[i+<‘9’))||((line[i+>=‘A’) && (line[i+<‘Z’)) || ((line[i+>=‘a’) && (line[i+<‘z’))
putchar(line[i]+1);
else if (line[i+ == ‘9’)
putchar(‘0’);
else if (line[i+ == ‘Z’)
putchar(‘A’);
else if (line[i+ == ‘z’)
putchar(‘a’);
else
putchar(‘.’);
}
Input: IIT Guwahati, 781039, Assam, India.
Output: JJU.Hvxbibuj..892140..Bttbn….Joejb.
ASCII
Character ASCII value
0 48
9 57
A 65
Z 90
a 97
z 122
Assignments
1. Read a string of alphabets (a to z or A to Z)
than replace each character with an
equivalent encoded character as follows.
2. A or a – 1
3. B or b – 2
26. Z or z- 26
Assignment
Solve the following algebraic Equation:
X5+3x2-10=0