0% found this document useful (0 votes)
4 views

Array 2

The document discusses strings in C programming. It defines strings as arrays of characters and describes common string operations like reading, writing, combining and comparing strings. It also provides examples of declaring and initializing string variables and using functions like gets() to read input as strings.

Uploaded by

aayusha766
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Array 2

The document discusses strings in C programming. It defines strings as arrays of characters and describes common string operations like reading, writing, combining and comparing strings. It also provides examples of declaring and initializing string variables and using functions like gets() to read input as strings.

Uploaded by

aayusha766
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

C H A R A C T E R A R R AYS

STRINGS
Strings
Definition
 A string is an array of characters.
 Any group of characters (except double quote sign) defined between
double quotation marks is a constant string.
 Character strings are often used to build meaningful and readable
programs.
The common operations performed on strings are
Reading and writing strings
Combining strings together
Copying one string to another
Comparing strings to another
Extracting a portion of a string ..etc.
05/21/2024 CSE 1001 Department of CSE 2
05/21/2024 CSE 1001 Department of CSE 3
05/21/2024 CSE 1001 Department of CSE 4
Strings
Declaration and initialization
char string_name[size];
The size determines the number of characters in the string_name.

For example, consider the following array:


char name [20];
is an array that can store up to 20 elements of type char.
It can be represented as:

05/21/2024 CSE 1001 Department of CSE 5


Strings

The character sequences "Hello" and "Merry Christmas"


represented in an array name respectively are shown as
follows :

05/21/2024 CSE 1001 Department of CSE 6


05/21/2024 CSE 1001 Department of CSE 7
05/21/2024 CSE 1001 Department of CSE 8
05/21/2024 CSE 1001 Department of CSE 9
Initialization of null-terminated character sequences
 arrays of characters or strings are ordinary arrays that
follow the same rules of arrays.

For example

To initialize an array of characters with some predetermined


sequence of characters one can initialize like any other array:

char myWord[ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };

05/21/2024 CSE 1001 Department of CSE 10


Initialization of null-terminated character sequences
 Arrays of char elements have an additional methods to initialize their values:
using string literals
 “Manipal ” is a constant string literal.
For example,
char result[14] =“Manipal”;
 Double quoted (") strings are literal constants whose type is in fact a null-
terminated array of characters.
So string literals enclosed between double quotes always have a null
character ('\0') automatically appended at the end.

05/21/2024 CSE 1001 Department of CSE 11


Initialization
 Initialization:

char myWord [ ] = { 'H', 'e', 'l', 'l', 'o', '\0' };

char myWord [ ] = "Hello";

 In both cases the array of characters myword is declared with a size of 6


elements of type char:

The 5 characters that compose the word "Hello" plus a final null
character ('\0') which specifies the end of the

sequence and that,

In the second case, when using double quotes (") null character ('\0') is
appended automatically.

05/21/2024 CSE 1001 Department of CSE 12


05/21/2024 CSE 1001 Department of CSE 13
Example
#include <stdio.h>

int main() {

char question[ ] = "Please, enter your first name: ";

char greeting[ ] = "Hello, ";

char yourname [ 80];

printf(“%s”,question);

scanf(“%s”, yourname);

printf(“%s…. %s\n”,greeting, yourname );


Please, enter your first name: Jey
return 0;
Hello, ... Jey
}
05/21/2024 CSE 1001 Department of CSE 14
Example

#include <stdio.h>

int main()

const int MAX = 80; //max characters in string

char str[MAX]; //string variable str

printf("Enter a string: \n");

scanf("%s",&str); //put string in str

printf("%s",str); //display string from str


Enter a string:
return 0;
gautam kumar
} gautam

05/21/2024 CSE 1001 Department of CSE 15


05/21/2024 CSE 1001 Department of CSE 16
05/21/2024 CSE 1001 Department of CSE 17
05/21/2024 CSE 1001 Department of CSE 18
05/21/2024 CSE 1001 Department of CSE 19
05/21/2024 CSE 1001 Department of CSE 20
Reading Embedded Blanks

To read everything that you enter from the keyboard until the
ENTER key is pressed (including space).
Syntax:
gets(string) ;

CSE 1001 Department of CSE 05/21/2024 21


Example
#include <stdio.h>

#include<string.h>

int main()

char str[80]; //string variable str

printf(“\nEnter a string: ”);

gets(str); //put string in str or use gets(str)

printf(“ the string is \n“);

puts(str); Enter a string: Manipal University Jaipur, India


The Entered String is
return 0; Manipal University Jaipur, India
}
05/21/2024 CSE 1001 Department of CSE 22
Count the number of characters in a string
#include <stdio.h> gets(sent);
puts(sent);

int main() while(sent[i]!='\0')

{ {

char sent[100]; count++;

int i=0, count=0; i++;


}

printf("enter sentence \n“); printf(“\n The No of characters=%d “, count);


return 0;
}

05/21/2024 CSE 1001 Department of CSE 23


Count the number of words in a sentence
#include <stdio.h>
while(sent[i]!='\0')
int main() {
if (sent[i]==' '&& sent[i+1]!=' ')
{
count++;
const int MAX = 100; i++;
}
char sent[MAX];
printf(“n %d n no. of words = " ,count);
int i=0,count=1; return 0;
}

printf("enter sentence \n“);


gets(sent);
printf("\n“);
Reading multiple lines: Example
#include <stdio.h>
gets(str);
int main()
printf("You entered:\n“);
{
printf(“%s”,str);
const int MAX = 2000;
return 0;
//max characters in string
}
char str[MAX]; //string

variable str

printf("\nEnter a string:\n“);

The function will continue to accept characters until enter key is pressed.

05/21/2024 CSE 1001 Department of CSE 25

You might also like