String Strings are actually onedimensional array of characters

  • Slides: 23
Download presentation
String : Strings are actually one-dimensional array of characters terminated by a null character

String : Strings are actually one-dimensional array of characters terminated by a null character ''. Thus a null-terminated string contains the characters that comprise the string followed by a null. The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello. “ char greeting[6] = {'H', 'e', 'l', 'o', ''};

If you follow the rule of array initialization then you can write the above

If you follow the rule of array initialization then you can write the above statement as follows − char greeting[] = "Hello"; Following is the memory presentation of the above defined string in C

Actually, you do not place the null character at the end of a string

Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '' at the end of the string when it initializes the array. Let us try to print the above mentioned string − #include <stdio. h> int main () { char greeting[6] = {'H', 'e', 'l', 'o', ''}; printf("Greeting message: %sn", greeting ); return 0; } When the above code is compiled and executed, it produces the following result Greeting message: Hello

Program to print a string in C #include <stdio. h> int main() { char

Program to print a string in C #include <stdio. h> int main() { char str[] = "Hello World"; printf("%sn", str); return 0; } Output : Hello World

Arrays of Strings #include<stdio. h> int main() { int j; char Names[5][9]={"Tejaswi", "Prasad", "Prasanth",

Arrays of Strings #include<stdio. h> int main() { int j; char Names[5][9]={"Tejaswi", "Prasad", "Prasanth", "Prakash", "Anand"}; for(j=0; j<5; j++) { printf("%sn", Names[j]); } return 0; }

String program to count vowels #include <stdio. h> int main() { char s[] =

String program to count vowels #include <stdio. h> int main() { char s[] = “SIESGST"; // String Given int i = 0; int vowels = 0; // Vowels counter int consonants = 0; // Consonants counter while(s[i++] != '') { if(s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' ) vowels++; else consonants++; } printf("'%s' contains %d vowels and %d consonants. ", s, vowels, consonants); return 0; }

String program to count characters #include <stdio. h> int main() { char s[] =

String program to count characters #include <stdio. h> int main() { char s[] = “SIESGST"; // String Given char ch = ‘S'; // Character to count i = 0; int count = 0; // Counter while(s[i] != '') { if(s[i] == ch) count++; i++; } if(count > 0) { if(count == 1) printf("%c appears %d time in '%s'", ch, count, s); else printf("%c appears %d times in '%s'", ch, count, s); }else printf("%c did not appear in %s", ch, s); return 0; }

Output of this program should be − a appears 3 times in ‘SIESGST'

Output of this program should be − a appears 3 times in ‘SIESGST'

Program to find string length without function in C #include <stdio. h> int main()

Program to find string length without function in C #include <stdio. h> int main() { char s 1[] = “SIESGST"; int i = 0; while(s 1[i] != '') { i++; } printf("Length of string '%s' is %d", s 1, i); return 0; } Output Length of string ‘SIESGST' is 7

Output : ‘SIESGST' contains 2 vowels and 5 consonants.

Output : ‘SIESGST' contains 2 vowels and 5 consonants.

Program to Sort String Characters in C

Program to Sort String Characters in C

#include <stdio. h> #include <string. h> int main ( ) { char string[] =

#include <stdio. h> #include <string. h> int main ( ) { char string[] = “SIESGST"; char temp; int i, j; int n = strlen(string); printf("String before sorting - %s n", string); for (i = 0; i < n-1; i++) { for (j = i+1; j < n; j++) { if (string[i] > string[j]) { temp = string[i]; string[i] = string[j]; string[j] = temp; } } } printf("String after sorting - %s n", string); return 0; }

Output of this program should be − String before sorting - SIESGST String after

Output of this program should be − String before sorting - SIESGST String after sorting - EGISSST

#include<stdio. h> void reverse(char b[100], int); int main() { int n=0; char a[100]; printf("Enter

#include<stdio. h> void reverse(char b[100], int); int main() { int n=0; char a[100]; printf("Enter a string : "); gets(a); while(a[n]!='') { n++; } reverse(a, n); return 0; }

void reverse(char b[100], int n) { int i; char temp; for(i=0; i<n/2; i++) {

void reverse(char b[100], int n) { int i; char temp; for(i=0; i<n/2; i++) { temp=b[n-1]; b[n-1]=b[i]; b[i]=temp; } printf("The reverse of this string is : %s", b); }

strlen ( ) function #include <stdio. h> #include <string. h> int main () {

strlen ( ) function #include <stdio. h> #include <string. h> int main () { char str[50]; int len; strcpy(str, "This is SIES GST best college in world"); len = strlen(str); printf("Length of %s is %dn", str, len); } return 0;

Example of strcat ( ) function #include <stdio. h> #include <string. h> int main

Example of strcat ( ) function #include <stdio. h> #include <string. h> int main () { char src[50], dest[50]; strcpy(src, "This is source"); strcpy(dest, "This is destination"); strcat(dest, src); printf("Final destination string : %s", dest); return(0); }

#include <stdio. h> #include <string. h> int main () { char str 1[15]; char

#include <stdio. h> #include <string. h> int main () { char str 1[15]; char str 2[15]; int ret; strcpy(str 1, "abcdef"); strcpy(str 2, "ABCDEF"); ret = strcmp(str 1, str 2); if(ret < 0) { printf("str 1 is less than str 2"); } else if(ret > 0) { printf("str 2 is less than str 1"); } else { printf("str 1 is equal to str 2"); } return(0); }

Write a program to check whether the entered string is palindrome or not (May

Write a program to check whether the entered string is palindrome or not (May 2013 Dec 2013, 2014) #include<stdio. h> int main() { int n=0, i; char a[100], rev[100]; printf("Enter a string : "); gets(a); while(a[n]!='') { n++; } for(i=0; i<n; i++) rev[n-i-1]=a[i]; for(i=0; i<n; i++) { if(a[i]!=rev[i]) break; } if(i==n) printf("The string is palindrome"); else printf("The string is not palindrome"); return 0; }

Concatenation of the string

Concatenation of the string

#include <stdio. h> #define MAX_SIZE 100 // Maximum string size int main() { char

#include <stdio. h> #define MAX_SIZE 100 // Maximum string size int main() { char str 1[MAX_SIZE], str 2[MAX_SIZE]; int i, j; /* Input two strings from user */ printf("Enter first string: "); gets(str 1); printf("Enter second string: "); gets(str 2); /* Move till the end of str 1 */ i=0; while(str 1[i] != '') { i++; }

/* Copy str 2 to str 1 */ j = 0; while(str 2[j] !=

/* Copy str 2 to str 1 */ j = 0; while(str 2[j] != '') { str 1[i] = str 2[j]; i++; j++; } // Make sure that str 1 is NULL terminated str 1[i] = ''; printf("Concatenated string = %s", str 1); return 0; }

Concatenation using function #include <stdio. h> #include <string. h> int main() { char a[1000],

Concatenation using function #include <stdio. h> #include <string. h> int main() { char a[1000], b[1000]; printf("Enter the first stringn"); gets(a); printf("Enter the second stringn"); gets(b); strcat(a, b); printf("String obtained on concatenation is %sn", a); return 0; }