Lecture CSP10
Lecture CSP10
Declaration of strings:
char str[30];
char line[80];
String Initialization:
char str[9] = “I like C”;
same as
char str[9]={‘I’,‘ ‘,’l’,‘i’,’k’,’e’,‘ ‘,’C’,’\0’};
Output???
C Programming Formatting String Output: %w.ns
w is the width of the string
C Pro dot (.) character after w and n are optional
if present only n characters will be displayed and
C Pro (w-n) leading spaces will be added before the string
if only width of the string(i.e w) is specified &
length of the string is less than the width specified then
the output will be right-justified with leading spaces.
and Otherwise, no leading space is added
Reading a String (contd..)
Using scanf()
char text[30];
printf(“Enter a string: ”);
scanf(“%s”,text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello
The string is: hello
-----------------------------------------
Enter a string: hello how are you
The string is: hello
Note: scanf() takes string without blank space
Reading a String (contd..)
char text[30];
printf(“Enter a string: ”);
scanf(“%[a-z]s”,text);
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello
The string is: hello
Enter a string: hello123
The string is: hello
Single Line input
char text[80];
printf(“Enter a string: ”);
scanf(“%[^\n]s”,text);/*newline terminated string */
printf(“The string is : %s”,text);
Sample output:
Enter a string: hello how are you
The string is: hello how are you
Multi line Input
char text[180];
printf(“Enter a string terminate with ~: ”);
scanf(“%[^~]s”,text);
printf(“The string is : %s”,text);
Output
Enter a string :C programming
The string is : C programming
Character Manipulation in the String
Example: Text without blank space
int main()
{ char s[80],ws[80];
int i,j;
printf(“Enter the text:\n”);
gets(s); /* reading text from user */
for(i=0,j=0; s[i]!=‘\0‘; i++)
{ if(s[i]!=‘ ‘)
ws[j++] = s[i];
}
ws[j]=‘\0’;
strcat(s1,s2) /* concatenates s2 to s1 */
text[n++]=‘\0’;
printf(“Length of text : %d”,n);
Implementation of strcat()
void main() /* s2 is concatenated after s1 */
{ char s1[100],s2[100];
int i = 0,j = 0;
printf("Enter first string\n");
scanf("%[^\n]",s1); getchar();
printf("Enter second string\n");
scanf("%[^\n]",s2);
while(s1[i++] != '\0'); i--;
while(s2[j] != '\0')
s1[i++] = s2[j++];
s1[i] = '\0';
printf("\n Final string is:%s",s1);
}
Palindrome problem: Algorithm Design
Returns false
If str[left] != str[right] for any value of left>=0 and right<=n-1
Where left<right
Palindrome problem: Implementation
void main()
{ char str[80];
int left,right,i,len,flag = 1;
printf("Enter a string");
for(i = 0;(str[i] = getchar())!='\n';++i);
len = i-1;
for(left = 0,right = len; left < right; ++left,--right)
{ if(str[left]!= str[right])
{ flag = 0;
break;
}
}
if(flag)printf("\n String is palindrome");
else
printf("\n String is not a palindrome");
}
Exercise: Write equivalent while loop for the above.
Word counting Problem: Algorithm
Problem Statement: Count the number of words in text.
Assumption a word is separated by blank space except
last word which is separated by ‘\0’.
Algorithm Steps
1. Read a character array text[N]
2. Initialize count =0
3. Repeat while text[i]!=‘\0’
If text[i] = ‘ ‘ then Increment count by 1
4. Print the count
5. Return
17
Word counting Problem: Implementation
void main()
{ char text[40];
int i = 0,count = 0;
printf("Enter a string:");
gets(text);
while(text[i]!='\0')
{
while(isspace(text[i]))
i++; /* Repeat till first non blank character */
if(text[i]!='\0')
{ count++;
while(!isspace(text[i])&& text[i]!='\0')
i++; /* Repeat till first blank character */
}
}
printf("The number of words in the string is %d",count);
}
18
Home assignment Problem
1. Write a program to count the number of
characters and words in a text string.
2. Write a program to reverse a string.
3. Write a program which will read a line of text and
rewrite it in the alphabetical order.
4. Write a program to copy one string into another
string.
5. Write a program to replace a particular word by
another word in a given string. Both the words are
provided by the user at run time.
19