Beruflich Dokumente
Kultur Dokumente
#include <stdio.h>
#include <string.h>
main()
ASCII value of
main()
{
int main(void){
char str1[] = "Hello" ;
//char str2[10] ;
char *s = "Good Morning" ;
char *q ;
IT IS NOT POSSIBLE TO DIRECTLY ASSIGN A STRING TO ANOTHER, ESPECIALLY ONE THAT HAS
BEEN INITIALISED ALREADY. IF THE STRING IS UNINITIALIZED, THEN IT CAN BE DONE AS
FOLLOWS USING POINTERS.
hello goodbye hello 4206628 4206634 4206628 hello goodbye goodbye 4206628 4206634
4206634
Important to retain end-of-string marker
(replacing str1[5] in the
original string with something other than ‘\0’
makes a string that
does not end)
TO READ A STRING CHARACTER BY
CHARACTER
OUTPUT:
strcpy()
• Works very much like a string assignment
operator.
char *strcpy (char *str1, char *str2);
– Assigns the contents of str2 to str1.
– Returns address of the destination string.
strlen()
• Counts and returns the number of characters
in a string.
int strlen (char *str);
len = strlen (string);
/* Returns an integer */
– The null character (‘\0’) at the end is not counted.
– Counting ends at the first null character.
char line[45];
char name[] = "ankita radhakrishnan";
printf("%d is the size of %s " , sizeof(name) , name);
printf("%d is the size of %s " , strlen(name) , name);
printf("\n%u %u\n" , name, line);
printf("%u\n" , strcpy(line, name));
OUTPUT
21 is the size of ankita radhakrishnan 20 is the size of ankita radhakrishnan
6356686 6356707
6356707
strcmp()
• Compares two character strings.
int strcmp(char *str1, char *str2);
– Compares the two strings and returns 0 if they
are identical; non-zero otherwise.
char line[45];
Printing to a String
• The sprintf function allows us to print to a string
argument using printf formatting rules
• First argument of sprintf is the string to print to,
remaining arguments are as in printf
char buffer[100];
sprintf (buffer, "%s %s %s %d %f", LastName, FirstName , "12-02-1993" , 26 ,
9.75);
printf("%s" , buffer);
if (strlen(buffer) > 15)
printf("Long name %s %s\n", FirstName, LastName);
strtok() function
char name[45];
scanf("%[^\n]s" , name);
Functions Description