0% found this document useful (0 votes)
9 views8 pages

Lecture 18

Strings are arrays of characters that can be manipulated and operated on using special string functions in C. Strings can be concatenated by copying the characters of one string to the end of another. The length of a string can be found by incrementing a counter as each character is iterated until the null terminator is reached. Strings can be compared for equality by checking each character for equality until a non-matching character or null terminator is found. User input for multiple strings can be read using scanf and the %s format specifier.

Uploaded by

Osama Ramadan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views8 pages

Lecture 18

Strings are arrays of characters that can be manipulated and operated on using special string functions in C. Strings can be concatenated by copying the characters of one string to the end of another. The length of a string can be found by incrementing a counter as each character is iterated until the null terminator is reached. Strings can be compared for equality by checking each character for equality until a non-matching character or null terminator is found. User input for multiple strings can be read using scanf and the %s format specifier.

Uploaded by

Osama Ramadan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Computing fundamentals

Working with strings


Strings are
basically char arrays
with special extra functions
#include <stdio.h> Find the output
void concat (char result[], const char str1[], int n1, const char str2[], int n2)
{
int i, j;
// copy str1 to result
for ( i = 0; i < n1; ++i )
result[i] = str1[i]; 3ala Rasiii
// copy str2 to result
for ( j = 0; j < n2; ++j )
result[n1 + j] = str2[j];
}
int main (void)
{
char s1[5] = {'3', 'a', 'l', 'a', ' \0'}; // the \0 denotes the end of the string
char s2[6] = {'R', 'a', 's', 'i', 'i', ‘i‘, \0' };
char s3[11];
int i;
concat (s3, s1, 5, s2, 6);
for ( i = 0; i < 11; ++i )
printf ("%c", s3[i]);
printf ("\n");
return 0; }
#include <stdio.h> Variable length character strings
int stringLength (const char string[])
{
int count = 0;
while ( string[count] != '\0' )
++count; 5 2 3
return count;
}
int main (void)
{
char word1[] = {'s', 'a', 'l', 'a', 'm', '\0' }; // the \0 denotes the end of the string
char word2[] = {'3', 'a', '\0' };
char word3[] = {'l', 'y', 'k', '\0' };
printf ("%i %i %i\n", stringLength (word1),stringLength (word2), stringLength
(word3));
return 0;
}
char word[] = "Hello!"; is perfectly equivalent to char word[] = { 'H', 'e', 'l', 'l', 'o', '!', '\0' };
Difference between quotation types
• Strings are arrays of characters

• Which of the following is correct?

char x= "A"; The double quotation marks are used to delimit the character
string, which can contain any combinations of letters, numbers,
char x= 'A'; or special characters
printf(" Hello ");
To assign a single character to such a variable, the character is
printf(' Hello '); enclosed within a pair of single quotation marks
#include <stdio.h> Concatenating strings
void concat (char result[], const char str1[], const char str2[]);
int main (void)
{
const char s1[] = { “Jibt " };
const char s2[] = { “5obiz???" };
Jibt 5obiz???
char s3[20];
concat (s3, s1, s2);
printf ("%s\n", s3);
return 0;
}
// Function to concatenate two character strings
void concat (char result[], const char str1[], const char str2[])
{
int i, j;
for ( i = 0; str1[i] != '\0'; ++i ) // copy str1 to result
result[i] = str1[i];
for ( j = 0; str2[j] != '\0'; ++j ) // copy str2 to result
result[i + j] = str2[j];
result [i + j] = '\0'; // Terminate the concatenated string with a null character
}
#include <stdio.h>
#include <stdbool.h> //makes it easy to work with bool
Are strings equal??
bool equalStrings (const char s1[], const char s2[])
{
int i = 0;
bool areEqual;
while ( s1[i] == s2 [i] && s1[i] != '\0' && s2[i] != '\0' )
++i;
if ( s1[i] == '\0' && s2[i] == '\0' )
areEqual = true;
else 0
areEqual = false; 1
return areEqual; 1
}
int main (void)
{
char stra[] = “esm3";
char strb[] = “m3allem";
printf ("%i\n", equalStrings (stra, strb));
printf ("%i\n", equalStrings (stra, stra));
printf ("%i\n", equalStrings (strb, " m3allem "));
return 0;
}
Input strings from user
// Program to illustrate the %s scanf format characters
#include <stdio.h>
Enter text:
int main (void) Elmaddeh men
el2aa5er
{ s1 = Elmaddeh
s2 = men
char s1[], s2[], s3[]; s3 = el2aa5er
printf ("Enter text:\n");
scanf ("%s%s%s", s1, s2, s3);
printf ("\ns1 = %s\ns2 = %s\ns3 = %s\n", s1, s2, s3);
return 0;
}

You might also like