Presentation is loading. Please wait.

Presentation is loading. Please wait.

C Strings Prabhat Kumar Padhy

Similar presentations


Presentation on theme: "C Strings Prabhat Kumar Padhy"— Presentation transcript:

1 C Strings Prabhat Kumar Padhy
1 1

2 C Strings? S R I C I T Y \0 S R I C I T Y \0
Strings ? – Is not this a set of characters ? Character arrays are often called as strings. String is always terminated by ‘\0’ - NULL main() { static char name[] = “SRI CITY”; int i=0; while (name[i] = ‘\0’) printf(“%c”,name[i]); i++; } ASCII value of ‘\0’ is 0 A string can also be initialized as static char[] = “SRI CITY”; main() { static char name[] = “SRI CITY”; int i=0; char *ptr; ptr = name; //ptr = &name[0]; while (ptr = ‘\0’) printf(“%c”, ptr); ptr++; } S R I C I T Y \0 S R I C I T Y \0

3 C Stings? String elements can also be accessed by the use of Pointers
char name[5][100]; C Stings? String elements can also be accessed by the use of Pointers printf and scanf possess simple way of handling string I/O Main() { char name[25]; scanf(“%s”,name); // format specification for string is %s printf(“%s”,name); // format specification for string is %s } String can be one dimensional character array or two dimensional character array char name[5][100]; each line in the 2D array is an array of char with size = 100 The above declaration can also be done as below char *name [100];

4 Pointer, Array / Strings?
char name[5][100]; Pointer, Array / Strings? Pointers and arrays are very closely linked in C. Think of array elements arranged in consecutive memory locations. For Ex: char a[10],*p; P = &a[0]; // pointer “p”, points to address of a[0] P p++ There is a subtle difference between pointer and array, such as we can write p=a instead p = &a[0] a[i] can be written as using pointer *(p+i) => &a[i] = (p+i) However pointers and arrays are different Pointer is a variable (p=a; p++; ….) Array is not a variable, rather a Datatype or data structure  Important point to understand S R I C I T Y \0

5 Array of Pointers to Strings?
char name[5][100]; Array of Pointers to Strings? A pointer, points to string. Set of such pointers pointing to different strings in an array, then it is array of pointers to string 6001 6009 6014 6018 Array of pointers names[] void main() { static char names[] = { “SRI CITY”, “IIIT”, “IIT”, IIT TPTY”, }; Int I; for(i=0;i<3;i++) printf(“%s\n”,names[i]); } S R I C I T Y \0 I I I T\0 I I T \0 I I T T P T Y \0

6 Basic String Handling Function
Syntax Purpose strlen Size_t strlen(const char *str) Finds length of a string strcpy Char * strcpy(char *string1,const char *string2) Copy string2 to string1.  strncpy char *strncpy(char *string1,const char *string2, size_t n) Copy first n characters of string2 to stringl strcasecmp int strcasecmp(const char *s1, const char *s2) It can compare the with case strncat char *strncat(char *string1, char *string2, size_t n) N no of charactrs Strcmp int strncmp(char *string1, char *string2, size_t n Strncmp

7 Strlen without using builtin function?
/* C program to find the length of a string without using the built-in function */ #include <stdio.h> void main() { char string[50]; int i, length = 0; printf("Enter a string \n"); gets(string); /* keep going through each character of the string till its end */ for (i = 0; string[i] != '\0'; i++) length++; } printf("The length of a string is the number of characters in it \n"); printf("So, the length of %s = %d\n", string, length);

8 Examples / Questions? Ex-3 Ex-1 EX - 2 int main () {
short number,sum; int bignumber,bigsum; char letter;   main() {   } Examples / Questions? Ex-3 int main () { static char str[] = “This is IIITs”; char *s; s=&str[6]-6; while(*s) printf(“%c”,*s++); return 0; } Ex-1 int main () { static char s[] = “This is IIITs”; printf(“%d”,*(s+strlen(s))); return 0; } EX - 2 int main () { char ch[20]; int i; for(i=0;i<19;i++) *(ch+i) = 67; *(ch+i)=‘\0’; printf(“%s”,ch); return 0; }

9 Examples / Questions? Ex-6 Ex-4 EX - 5 void main() {
short number,sum; int bignumber,bigsum; char letter;   main() {   } Examples / Questions? Ex-6 Ex-4 void main() { static char *names[] = { “SRI CITY”, “IIIT”, “IIT”, IIT TPTY”, }; int i; printf(“%d%d\n”, sizeof(names),sizeof(names[i])); } EX - 5

10 Strlen without using builtin function?
/* C program to find the length of a string without using the built-in function */ #include <stdio.h> void main() { char string[50]; int i, length = 0; printf("Enter a string \n"); gets(string); /* keep going through each character of the string till its end */ for (i = 0; string[i] != '\0'; i++) length++; } printf("The length of a string is the number of characters in it \n"); printf("So, the length of %s = %d\n", string, length);

11 Questions?


Download ppt "C Strings Prabhat Kumar Padhy"

Similar presentations


Ads by Google