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...

Stack Data Structure in Cpp (c++)

  What is Stack? Stack refers to an abstract data types (ADTs) that is a collection of elements, with two principal operations that are addition and removal of elements from the collection. It works on the principle of LIFO (last In first out). It is also considered as a linear data structure.   Example: A set of books. It easy to take an item off the top rather from the middle. Basic operations of Stack: Push: Add item to the list. Pop: Removes item from the list.   Advantages: Stack is faster than heap memory allocation (also known as Dynamic Memory Allocation). Stack memory is automatically reclaimed when the function exits, which can be convenient for the programmer if the data is no longer required. Therefore, it is suitable for temporary data, where memory is not needed after the execution of a function. Implementation Of Stack in C++: #include<iostream> using namespace std; class StackMemory...

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