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

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

Single LinkList in Java

Linked List: Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. LinkedList − A Linked List contains the connection link to the first link called First. Types of Linked List: Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations: Insert:  Inserts at tail,  specific index. Delete: Deletes from the tail.  specific index. ...

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...