3. Printing with puts( )
• For example:
char sentence[] = "The quick brown fox";
puts(sentence);
• Prints out:
The quick brown fox
Dr. Yousaf, PIEAS
4. #include <stdio.h>
int main()
{
char name1[10]={'y', 'o','u','s','a','f','0'} ;
char name2[10] = "Shahid";
printf("Name1 = %cn", name1[3]); // note the use of %c
printf("Name1 = %sn", name1); // note the use of %s
printf("Name2 = %cn", name2[3]);
printf("Name2 = %sn", name2);
puts(name2);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
5. Inputting String with scanf
• Inputting strings
– Use scanf
scanf("%s", word);
// note the use of %s, no use of & sign
• Copies input into word[]
• Do not need & (because a string is a pointer
(Later))
– Remember to leave room in the array for '0'
// Compare it with
scanf(“%c",& x); // note
the use of %c
Dr. Yousaf, PIEAS
6. #include <stdio.h>
int main()
{
char fname[20], lname[20];
puts("Please enter your first name (maximum 20 charters) and
then please press enter");
puts("Please enter your Last Name (maximum 20 charters) and
then please press enter");
scanf("%s%s", fname, lname); // to read one or more strings
printf("nWelcome Mr. %s %s", fname,lname);
getchar(); return 0; }
Dr. Yousaf, PIEAS
Inputting Strings with scanf ( )
7. Inputting Strings with scanf ( )
• To read a string include:
– %s scans up to but not including the “next” white space character
– %ns scans the next n characters or up to the next white space
character, whichever comes first
• Example:
scanf ("%s%s%s", s1, s2, s3); // when ever space would be
entered, scan for the next string will strat
scanf ("%2s%3s%2s", s1, s2, s3); //
– Note: No ampersand(&) when inputting strings into character
arrays! (We’ll explain why later …)
• Difference between gets
– gets( ) reads a line
– scanf("%s",…) read up to the next space
Dr. Yousaf, PIEAS
8. An Example
#include <stdio.h>
int main ()
{
char lname[81], fname[81];
int count, id_num;
puts ("Enter the last name, firstname, ID number separated by
spaces, then press Enter n");
count = scanf ("%s%s%d", lname, fname,&id_num);
printf ("%d items entered: %s %s %dn",
count,fname,lname,id_num);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
9. #include <stdio.h>
int main()
{
char name1[20];
puts("Please enter your name , maximum 20 characters and then
please press enter");
gets(name1); // get string, it takes only one string
printf("Name1 = %sn", name1); // note the use of %s
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Inputting Strings with gets( )
10. Inputting Strings with gets( )
• gets( ) gets a line from the standard input.
• char your_line[100];
printf("Enter a line:n");
gets(your_line);
puts("Your input follows:n");
puts(your_line);
– You can overflow your string buffer, so be careful!
Dr. Yousaf, PIEAS
11. Inputting String with scanf
• Inputting strings
– Use scanf
scanf("%s", word);
// note the use of %s, no use of & sign
• Copies input into word[]
• Do not need & (because a string is a pointer
(Later))
– Remember to leave room in the array for '0'
// Compare it with
scanf(“%c",& x); // note
the use of %c
Dr. Yousaf, PIEAS
12. #include <stdio.h>
int main()
{
char fname[20], lname[20];
puts("Please enter your first name (maximum 20 charters) and
then please press enter");
puts("Please enter your Last Name (maximum 20 charters) and
then please press enter");
scanf("%s%s", fname, lname); // to read one or more strings
printf("nWelcome Mr. %s %s", fname,lname);
getchar(); return 0; }
Dr. Yousaf, PIEAS
Inputting Strings with scanf ( )
13. Inputting Strings with scanf ( )
• To read a string include:
– %s scans up to but not including the “next” white space character
– %ns scans the next n characters or up to the next white space
character, whichever comes first
• Example:
scanf ("%s%s%s", s1, s2, s3); // when ever space would be
entered, scan for the next string will strat
scanf ("%2s%3s%2s", s1, s2, s3); //
– Note: No ampersand(&) when inputting strings into character
arrays! (We’ll explain why later …)
• Difference between gets
– gets( ) reads a line
– scanf("%s",…) read up to the next space
Dr. Yousaf, PIEAS
14. An Example
#include <stdio.h>
int main ()
{
char lname[81], fname[81];
int count, id_num;
puts ("Enter the last name, firstname, ID number separated by
spaces, then press Enter n");
count = scanf ("%s%s%d", lname, fname,&id_num);
printf ("%d items entered: %s %s %dn",
count,fname,lname,id_num);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
15. #include <stdio.h>
int main()
{
char name1[20];
puts("Please enter your name , maximum 20 characters and then
please press enter");
gets(name1); // get string, it takes only one string
printf("Name1 = %sn", name1); // note the use of %s
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Inputting Strings with gets( )
16. Inputting Strings with gets( )
• gets( ) gets a line from the standard input.
• char your_line[100];
printf("Enter a line:n");
gets(your_line);
puts("Your input follows:n");
puts(your_line);
– You can overflow your string buffer, so be careful!
Dr. Yousaf, PIEAS
18. The C String Library
Use #include <string.h>
– Includes functions such as:
• Computing length of string
• Copying strings
• Concatenating strings
• These techniques used to make
– Word processors
– Page layout software
– Typesetting programs
Dr. Yousaf, PIEAS
19. strlen
• char str[15] = "unix and c";
• printf("length = %d", strlen(str) );
Output: Length = 10
• int sgl;
char str[15] = "unix and c";
sgl = strlen(str)
printf("length = %d", sgl );
Dr. Yousaf, PIEAS
20. A copy of source is made at destination
– destination should have enough room
(its length should be at least the size of source)
Dr. Yousaf, PIEAS
String Copy (strcpy)
21. String Copy (strcpy)
// String Copy strcpy
#include <stdio.h>
#include <string.h> // it must be included for string
operations
int main()
{
char name1[20] = "Yousaf";
char name2[20];
strcpy(name2,name1); // copies name1 to name2
printf("Name1 = %sn", name1);
printf("Name2 = %sn", name2);
getchar();
return 0; }
Dr. Yousaf, PIEAS
22. • Ensure that str1 has sufficient space for the
concatenated string!
– Array index out of range will be the most
popular bug in your C programming career.
Dr. Yousaf, PIEAS
String Concatenation (strcat)
24. String Upper Case and Lower Case
#include <stdio.h>
#include <string.h>
int main()
{
char name[20] = "HeLLo";
puts(name);
puts(strlwr(name)); // hello
puts(strupr(name)); // HELLO
getchar();
return 0;
}
Dr. Yousaf, PIEAS
25. Example
#include <string.h>
#include <stdio.h>
int main() {
char str1[27] = "abc";
char str2[100];
printf("%dn",strlen(str1));
strcpy(str2,str1);
puts(str2);
puts("n");
strcat(str2,str1);
puts(str2);
}
Dr. Yousaf, PIEAS
26. Character Searching (strchr)
#include<stdio.h>
#include<string.h>
int main()
{
char name[20]= "Yousaf";
char x = ‘b';
if (strchr(name, x) == NULL)
printf ("The character %c was not found.n",x);
else
printf ("The character %c was found", x);
//Rest of the code
Dr. Yousaf, PIEAS