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

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

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