STRINGS:
A string is a series of characters treated as a single unit. A string may include letters, digits and various special characters such as +, -, *, / and $. String literals or string constants in C are written in double quotation marks.STRING DECLARATION:
A string in C is implemented as an array, so declaring a string variable is the same as declaring an array of type char.Syntax:
char var[9] = {'S','T','R','I','N','G','S','!'};OR
char string_var[9] = "STRINGS!";
INTERPRETATION:
The variable var will hold strings from 0 to 8 characters long. The variable string_var will hold strings from 0 to 8 characters longNULL CHARACTER (‘\0’):
Null character marks the end of a string.All of C’s string handling functions simply ignore whatever is stored in the cells following the null character.
When defining a character array to contain a string, the array must be large enough to store the string and its terminating null character.
ARRAYS OF STRINGS:
One string is an array of characters, an array of strings is a two-dimensional array of characters in which each row is one string.An array of strings can be initialized at declaration in the following manner:
char day[7][10] = {"Sunday" , "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
String Library Functions<string.h>:
String Library Functions<ctype.h>:
Implementation of Strings in C:
Q: Write a function named String_Manipulation() which receives a string and returns the results after performing the following operations.- sum of characters(ascii values)
- copy one string to another
- concatenate two strings
- convert lower case string to upper and vice versa.
/* QUESTION#2 Write a function named String_Manipulation() which receives an string and returns the results after performing the following operations. sum of characters(ascii values) copy one string to another concatenate two strings convert lower case string to upper and vice versa. Call this function from main. Use appropriate parameters and return type. */ #include<stdio.h> #include<string.h> #include<malloc.h> void String_Manipulation(char *ptrstr,int size) { int x,y,sumascii=0; for (x=0;x<size;x++) { y=ptrstr[x]; sumascii +=y; } printf("The sum of characters(ascii values) is \n%d\n\n",sumascii); char *ptrstr2; ptrstr2=(char *)malloc(sizeof(char)*50); strcpy(ptrstr2,ptrstr); puts("string copied"); printf("%s\n",ptrstr2); puts(""); puts("string in lower case"); strlwr(ptrstr); puts(ptrstr); puts(""); puts("string in upper case"); strupr(ptrstr); puts(ptrstr); puts(""); puts("concatenate two strings"); strcat(ptrstr2,ptrstr); puts(ptrstr2); free(ptrstr2); } int main() { char *ptrstr; int size; ptrstr=(char *)malloc(sizeof(char)*50); puts("Enter name"); gets(ptrstr); size=strlen(ptrstr)+1; String_Manipulation(ptrstr,size); free(ptrstr); }
Q: Write a C program that asks the user for house address in the format HouseNumber, Block-Number, Area, City, Country.
The application takes as an input this address, parses the address and replies to the user with House
Number, Block, Area’s name , City and Country.
/* QUESTION#2 Write a function named String_Manipulation() which receives an string and returns the results after performing the following operations. sum of characters(ascii values) copy one string to another concatenate two strings convert lower case string to upper and vice versa. Call this function from main. Use appropriate parameters and return type. */ #include<stdio.h> #include<string.h> #include<malloc.h> void String_Manipulation(char *ptrstr,int size) { int x,y,sumascii=0; for (x=0;x<size;x++) { y=ptrstr[x]; sumascii +=y; } printf("The sum of characters(ascii values) is \n%d\n\n",sumascii); char *ptrstr2; ptrstr2=(char *)malloc(sizeof(char)*50); strcpy(ptrstr2,ptrstr); puts("string copied"); printf("%s\n",ptrstr2); puts(""); puts("string in lower case"); strlwr(ptrstr); puts(ptrstr); puts(""); puts("string in upper case"); strupr(ptrstr); puts(ptrstr); puts(""); puts("concatenate two strings"); strcat(ptrstr2,ptrstr); puts(ptrstr2); free(ptrstr2); } int main() { char *ptrstr; int size; ptrstr=(char *)malloc(sizeof(char)*50); puts("Enter name"); gets(ptrstr); size=strlen(ptrstr)+1; String_Manipulation(ptrstr,size); free(ptrstr); }
Comments
Post a Comment