1 / 11

C Strings Prabhat Kumar Padhy

C Strings Prabhat Kumar Padhy. 1. C Strings?. ASCII value of ‘’ is 0 A string can also be initialized as static char[] = “SRI CITY”; 3001 3002 3003 3004 3005 3006 3007 3008 3009 main() { static char name[] = “SRI CITY”; int i =0; char * ptr ;

rosah
Download Presentation

C Strings Prabhat Kumar Padhy

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. C StringsPrabhat Kumar Padhy 1

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

  3. 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. 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] 3001 3002 3003 3004 3005 3006 3007 3008 3009 • 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. 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 6001 6009 6014 6018

  6. Basic String Handling Function

  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]; inti, 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. 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 - 2 int main () { char ch[20]; inti; for(i=0;i<19;i++) *(ch+i) = 67; *(ch+i)=‘\0’; printf(“%s”,ch); return 0; } • Ex-1 int main () { static char s[] = “This is IIITs”; printf(“%d”,*(s+strlen(s))); return 0; }

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

  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]; inti, 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?

More Related