introduction to strings in c programming

mikeymanjiro2090 134 views 18 slides Feb 29, 2024
Slide 1
Slide 1 of 18
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18

About This Presentation

a brief introduction too strings


Slide Content

/*Introduction to Strings*/ < C Programming > { } ...

Table of contents Strings, More about Strings…,Pointers & strings; stren(), strcyp(), strcat(), strcmp(); //Uk it 01 02 03 Introduction Standard Library String Functions Programs

Whoa! /* WHAT ARE STRINGS?? */ * }

Char name[]={‘H’,’A’,’E’,’S’,’L’,’E’,’R’,’\0’}; 01 The way a group of integers can be stored in an integer array,similarly a group of characters can be stored in a character array. A string is a 1-D array of characters terminated by a ' \0 '. ‘\0’ is called null character. ‘\0’ and ’0’ are not same. ASCII value of ‘\0’ is 0,whereas ASCII value of ‘0’ is 48. C concedes the fact that you would use strings very often and hence provides a shortcut for initializing strings; { } ..

/*MORE about Strings!*/ M Character Arrays: In C, a string is essentially an array of characters terminated by a null character ( '\0' ). This null character is used to mark the end of the string. For example: char myString[] = "Hello, World"; String Input : You can use functions like scanf or gets (not recommended due to security issues) to input strings from the user. For example: char name[50]; printf("Enter your name: "); scanf("%s", name); String Output : You can use functions like printf to display strings . For example: char greeting[] = "Hello, World"; printf("%s\n", greeting);

/*POINTERS and STRINGS!*/ Do you know Pointers and strings are fundamental concepts in programming, especially in languages like C and C++. It’s a variable that holds the address memory of another variable It allows you to indirectly access and manipulate data. This is how you declare it: C int*ptr; This declares a pointer ptr that can point to an integer. A string is an array of characters, typically terminated by a null character ('\0'). In C, strings are represented as arrays of characters. For example: char str[] = "Hello"; *

MORE ABOUT POINTERS… In this case, str is an array of characters containing the string "Hello". Pointers and strings often go together in C, as strings are typically manipulated using pointers. For instance, you might use a pointer to iterate through the characters of a string or to pass a string to a function. Remember that in C, strings are represented as arrays of characters, and there are various string functions available in the standard library (like strlen, strcpy, strcmp, etc.) to perform operations on strings. { } *

Standard Library Functions It returns the number of characters in a string.Syntax : int strlen (string name) I t is for copying source string into destination string •The length of the destination string >= source string.Syntax : strcpy (Destination string, Source String); strcat () It combines two strings. The length of the destination string must be > than the source string. strlen () strcpy() ... The predefined functions which are designed to handle strings are available in the library string.h.

Standard Library Functions This function compares 2 strings. •It returns the ASCII difference of the first two non – matching characters in both strcmp() ... s trrev () The  strrev ()  function is a built-in function in C and is defined in  string.h  header file. The strrev () function is used to reverse the given string . 

PROGRAMS /* Basic string program */ #include <stdio.h> #include<conio.h > Void main { int roll_no.; char name[20],address[20],branch[20] printf(“\n Enter roll no.”); scanf(“%d”, &roll_no.); printf(“enter name”); scanf(“%s”, name); printf(“\n student name is %s”, name ); getch (); }

PROGRAMS /* strlen() program */ #include <string.h> main (){ char a[30]=“Hello”; int l; l= strlen (a); printf (“length of the string=% d”,l ); getch (); }

PROGRAMS /* strcpy() program */ #include<string.h> main(){ char a[50]; printf(“Enter a source string”); scanf(“%s”,a); printf(“Enter destination string”); scanf(“%s”,b); strcpy(b,a); printf(“copied string=%s”,b); getch(); }

PROGRAMS /* strcat() program */ #include<string.h> main(){ char a[50]=“Hello”; char b[20]=“Good Morning”; clrscr(); strcat(a,b); printf(“concatenated string=%s”,a); getch(); }

PROGRAMS /* string using if else program */ #include<stdio.h> #include<string.h > int main(){ char a[50],b[50]; int d; printf(“Enter 2 strings:”); scanf(“%s %s”,a,b); d=strcmp(a,b); if(d==0){ printf(“%s is (alphabetically) equal to %S”,a,b); }else if (d>0){ printf(“%s is (alphabetically) greater than %s”,a,b); }else if (d<0){ printf(“%s is (alphabetically) less than %s”,a,b); }

/* strcmp () program*/ #include < stdio.h > #include < string.h > int main() { char str1[] = " abcd ", str2[] = " abCd ", str3[] = " abcd "; int result; /* comparing strings str1 and str2 */ result = strcmp (str1, str2); printf (" strcmp (str1, str2) = %d\n", result); /* comparing strings str1 and str3 */ result = strcmp (str1, str3); printf (" strcmp (str1, str3) = %d\n", result); return 0; } PROGRAMS

#include< stdio.h >   void  main ()   {     char  *p = "hello  javatpoint ";     printf ("String p: %s\ n",p );       char  *q;      printf ("copying the content of p into q...\n");     q  = p;     printf ("String q: %s\ n",q );     }   PROGRAMS

/* strrev () program*/ #include < stdio.h > #include < string.h > int main() { char str [50] = “C PROGRAMMING"; printf ("The given string is =%s\n", str ); printf ("After reversing string is =%s", strrev ( str )); return 0; } PROGRAMS

THANKYOU {… }…
Tags