Skip to main content

Strings

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 long

NULL 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.
Call this function from main. Use appropriate parameters and return type.

/* 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

Popular posts from this blog

Data Types

DATA TYPES in C/C++: Since C and C++ are strongly type languages, therefore every thing must be defined. Variables are classified according to their data type, which determines the kind of information that may be stored in them. Data Types Description C-Keyword Format Specifier integer Integers are whole numbers that can have both positive and negative values but no decimal values.   int   %d or %i Float Floating type variables can hold real numbers precision of 6 digits. float %f Double can hold real numbers with the precision of 14 digits. double %f Character data type allows a variable to store only one character. char %c Implementation in C: #include<stdio.h> int main() { printf(“%d n”,18); //this will print integer printf(“%fn”,32.56); //this will print decimal number printf(“%d %d %dn”,32,56,89); // %d is a place holder for integer and it provide place for one integer printf(“%ld...

Conditional Statement

Conditional Statement: A conditional statement is an if-then statement which means that if some statement is true then it follows the if-block of code and if the condition is false then it follows the else-block of code. In the programming world, there are two types of conditional statement namely if-else and switch. if-else Statement: Syntax: if (condition 1) { } else if (condition 2) { } else { }   Implementation of If-else statement in C: Q: Write a program to find the greatest of three numbers. #include<stdio.h> int main() { int a,b,c; printf(“Please Enter First Number “); scanf(“%d”,&a); printf(“Please Enter Second Number “); scanf(“%d”,&b); printf(“Please Enter Third Number “); scanf(“%d”,&c); if (a>b && a>c) { printf(“The Greatest Number is %d”,a); } else if (b>c) { printf(“The Greatest Number is %d”,b); } else { pr...

Tic-Tac-Toe Implementation in Cpp (C++) Step by Step