String Manipulation in C Programming - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

String Manipulation in C Programming

Share This



A string is nothing but an array of characters. Hence, a string can be initialized at the time of variable declaration or it can be taken as input from the user.

A string can be initialized as

char str[] = {'H', 'e', 'l', 'l', 'o'};
or

char str[] = "Hello";
Alternatively, the string can be taken as user input using standard input functions.

Remember that at the end of each string, a special symbol '\0' is added to specify the end of the string.

In this tutorial, we will discuss different operations performed on string data.

These operations are available in the "string.h" header file. But, here we will implement those functions using our own logic.


String Copy


The strcpy function of "string.h" header file can be used to copy a string to another string. In the following example, we are defining the function by which the content of str1 is copied to str2.
#include <stdio.h> void strcpy(char str1[], char str2[]); int main() { char str1[100], str2[100]; printf("Enter a string:"); scanf("%s", str1); strcpy(str1, str2); printf("%s", str2); return 0; } void strcpy(char str1[], char str2[]) { int i = 0; while(str1[i] != '\0') { str2[i] = str1[i]; i++; } str2[i] = '\0'; }

String Length


The strlen function of "string.h" header file can be used to determine the length of a string. In the following example, we are defining the function by which length of a string str is determined.

#include <stdio.h> int strlen(char str1[]); int main() { char str[100]; int len; printf("Enter a string:"); scanf("%s", str); len = strlen(str); printf("Length of the string is : %d", len); return 0; } int strlen(char str[]) { int len = 0; while(str[len] != '\0') { len++; } return len; }

String Concat


The strcat function of "string.h" header file is used to concatenate two strings and returns the combined string. In the following example, we are defining the function by which content of the second string is appended at the end of the first string and the combined string is returned.

#include <stdio.h> char * strcat(char str1[], char str2[]); int main() { char str1[100], str2[100], *res; printf("Enter first string:"); scanf("%s", str1); printf("Enter second string:"); scanf("%s", str2); res = strcat(str1, str2); printf("%s", res); return 0; } char * strcat(char str1[], char str2[]) { int i = 0, j = 0; char *res = (char *)malloc(200); res = str1; while(res[i] != '\0') i++; while(str2[j] != '\0') { res[i] = str2[j]; i++; j++; } res[i] = '\0'; return res; }

String Compare


It compares the two strings and returns an integer value. If both the strings are same (equal) then this function would return 0. If string1 < string2 OR string1 is a substring of string2 then it would result -1. If string1 > string2 then it would return positive value.

#include <stdio.h> int strcmp(char str1[], char str2[]); int main() { char str1[100], str2[100]; printf("Enter first string:"); scanf("%s", str1); printf("Enter second string:"); scanf("%s", str2); printf("%d", strcmp(str1, str2)); return 0; } int strcmp(char str1[], char str2[]) { int i = 0; while(str1[i] != '\0' && str2[i] != '\0' && str2[i] == str2[i]) { i++; } if ((str1[i] < str2[i])) return -1; else if ((str1[i] == str2[i])) return 0; else return (str1[i] - str2[i]); }

String Reverse 


The following function is defined to reverse a string. Let us consider a string "Hello", the function takes this string as argument and it returns "olleH".

#include <stdio.h> char * strrev(char str[]); int main() { char str[100], *rev; printf("Enter a string:"); scanf("%s", str); printf("%s", strrev(str)); return 0; } char * strrev(char str[]) { int i, len = 0; char *rev, temp; while(str[len] != '\0') len++; rev = (char *) malloc(len); for (i = 0; i < len / 2; i++) { temp = str[i]; str[i] = str[len - 1 - i]; str[len - 1 - i] = temp; } return str; }

A program to count different types of characters.


#include <stdio.h> #include <ctype.h> void scan_line(char line[], int *pv, int *pc, int *pd, int *pw, int *po); int main() { char line[80]; int vowel = 0; int consonant = 0; int digit = 0; int whitespc = 0; int other = 0; printf("\nEnter a line of text below : "); scanf("%[^\n]", line); scan_line(line, &vowel, &consonant, &digit, &whitespc, &other); printf("\nNo. of vowels : %d", vowel); printf("\nNo. of consonants : %d", consonant); printf("\nNo. of digits : %d", digit); printf("\nNo. of white space : %d", whitespc); printf("\nNo. of other symbols : %d", other); return 0; } void scan_line(char line[], int *pv, int *pc, int *pd, int *pw, int *po) { char c; int count = 0; while ((c = toupper(line[count])) != '\0') { if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') ++*pv; else if (c >= 'A' && c <= 'Z') ++*pc; else if (c >= '0' && c <= '9') ++*pd; else if (c == ' ' || c == '\t') ++*pw; else ++*po; ++count; } }

Program to convert full name to short name.

#include <stdio.h> #include <string.h> void main() { char text[80]; int i, l, s = 0; printf("\nEnter the full name: "); scanf("%[^\n]", text); l = strlen(text); for (i = l - 1; i >= 0; i--) { if (text[i] == ' ' || text[i] == '\t') break; else s++; } printf("\n\n"); printf("The short name is: "); for (i = 0; i <= l; i++) { if (i == 0) printf("%c.", text[i]); else if ((i < l - s - 1) && (text[i] == ' ' || text[i] == '\t')) printf("%c.", text[i + 1]); else if (i >= l - s) printf("%c", text[i]); } }

A simple way to prepare a menu


#include <stdio.h> char *menutext(int n); int main() { int str_number; for (str_number = 0; str_number < 13; str_number++) { printf("%s", menutext(str_number)); } return 0; } char *menutext(int n) { static char *t[] = { " -------------------------------------- \n", " | ++ MENU ++ |\n", " | ~~~~~~~~~~~~ |\n", " | (1) Edit Defaults |\n", " | (2) Print Charge Sheet |\n", " | (3) Print Log Sheet |\n", " | (4) Bill Calculator |\n", " | (q) Quit |\n", " | |\n", " | |\n", " | Please Enter Choice |\n", " | |\n", " -------------------------------------- \n" }; return (t[n]); }


Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.