0% found this document useful (0 votes)
6 views7 pages

C String Handling and Arrays Guide

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

C String Handling and Arrays Guide

c programming
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

String Handling in C

1. Reading Strings from Terminal

In C, strings are read from the keyboard using several input functions.

(a) scanf()

 Syntax:
 scanf("%s", str);
 Reads a single word (stops at space, tab, newline).

(b) gets() (Not recommended—unsafe)

 Reads an entire line including spaces until newline.


 Syntax:
 gets(str);

(c) fgets() (safe method)

 Reads a full line safely from input.


 Syntax:
 fgets(str, sizeof(str), stdin);

2. Writing Strings to Screen

(a) Using printf()

printf("%s", str);

(b) Using puts()

 Prints string and automatically inserts a newline.

puts(str);

3. Arithmetic Operations on Characters

Characters in C are internally stored as ASCII integer codes, so arithmetic operations are
allowed.

Examples:

char ch = 'A';
printf("%d", ch); // outputs 65
printf("%c", ch + 1); // outputs 'B'
Useful Cases:

 Convert lowercase to uppercase:


 char c = 'a';
 char u = c - 32; // 'a' (97) → 'A' (65)
 Check alphabet sequence:
 if (c >= 'A' && c <= 'Z')
 printf("Uppercase");

4. Comparison of Two Strings

Strings cannot be compared using == because that compares addresses.


The correct method is using library functions.

(a) strcmp()

 Returns:
o 0 → strings are equal

o <0 → first string is smaller

o >0 → first string is greater

Example:

if (strcmp(str1, str2) == 0)
printf("Strings are equal");
else
printf("Not equal");

(b) strncmp()

 Compares first n characters.

5. String-handling Functions (from <string.h>)

(a) strlen(str)

Returns length of string (excluding \0).

(b) strcpy(dest, src)

Copies one string to another.

(c) strcat(str1, str2)

Appends str2 to str1.


(d) strcmp(str1, str2)

Compares two strings.

(e) strrev() (Turbo C/C++ only)

Reverses a string (not part of ANSI C).

(f) strlwr() / strupr() (non-standard)

Converts to lowercase/uppercase.

Short Example Program

#include <stdio.h>
#include <string.h>

int main() {
char str1[50], str2[50];

printf("Enter first string: ");


fgets(str1, 50, stdin);

printf("Enter second string: ");


fgets(str2, 50, stdin);

printf("Length of first string = %d\n", strlen(str1));

if (strcmp(str1, str2) == 0)
printf("Strings are equal\n");
else
printf("Strings are not equal\n");

return 0;
}

Declaration and Initialization of One-Dimensional Arrays

(A) Declaration

data_type array_name[size];

Example:

int marks[5];
float prices[10];
char letters[20];
(B) Initialization

1. Compile-time Initialization

int marks[5] = {10, 20, 30, 40, 50};

Partial initialization:

int a[5] = {1, 2}; // remaining elements become 0

2. Run-time Initialization

Values entered by user:

for(int i=0; i<5; i++)


scanf("%d", &a[i]);

3. Declaration and Initialization of Two-Dimensional Arrays

(A) Declaration

data_type array_name[rows][columns];

Example:

int matrix[3][4];

(B) Initialization

1. Compile-time Initialization

int mat[2][3] = {
{1, 2, 3},
{4, 5, 6}
};

2. Run-time Initialization

for(int i=0; i<2; i++)


for(int j=0; j<3; j++)
scanf("%d", &mat[i][j]);

4. Declaring and Initializing String Variables

Method 1: Character Array

char name[10] = {'J', 'o', 'h', 'n', '\0'};


Method 2: String Literal

char name[10] = "John";


 The compiler automatically adds \0.

Method 3: Uninitialized

char city[20];

Reading Strings

scanf("%s", name); // reads single word


gets(name); // reads full line (unsafe)
fgets(name,20,stdin); // safe method

Writing Strings

printf("%s", name);
puts(name);

5. Example Programs Using Arrays

Program 1: Read and Print 1D Array

#include <stdio.h>

int main() {
int a[5], i;
printf("Enter 5 numbers:\n");

for(i=0; i<5; i++)


scanf("%d", &a[i]);

printf("You entered:\n");
for(i=0; i<5; i++)
printf("%d ", a[i]);

return 0;
}

Program 2: Sum of Elements of an Array

#include <stdio.h>

int main() {
int a[5], i, sum = 0;

for(i=0; i<5; i++) {


scanf("%d", &a[i]);
sum += a[i];
}

printf("Sum = %d", sum);


return 0;
}

Program 3: Read and Display a 2D Array (Matrix)

#include <stdio.h>

int main() {
int mat[2][3], i, j;

printf("Enter values:\n");
for(i=0; i<2; i++)
for(j=0; j<3; j++)
scanf("%d", &mat[i][j]);

printf("Matrix:\n");
for(i=0; i<2; i++) {
for(j=0; j<3; j++)
printf("%d ", mat[i][j]);
printf("\n");
}

return 0;
}

Program 4: Reading and Printing a String

#include <stdio.h>

int main() {
char name[20];

printf("Enter your name: ");


fgets(name, 20, stdin);

printf("Hello %s", name);

return 0;
}

Program 5: Array of Strings (2D char array)


#include <stdio.h>

int main() {
char names[3][20] = {"John", "Mary", "Peter"};

for(int i=0; i<3; i++)


printf("%s\n", names[i]);

return 0;
}

You might also like