|
C Programming Tutorials
Basics of C:
Facts about C
Why to Use C
C Program File
C Compilers
Program Structure:
Simple C Program
C
Program Compilation
Basic DataTypes:
DataTypes
Modifiers
Qualifiers
Arrays
Variable Types:
Local Variable
Global
Variable
Storage Classes:
auto storage class
register storage
class
static storage
class
extern storage
class
Using Constants:
Defining Constants
The enum Data Types
Operator Types:
Arithmetic Operators
Logical Operators
Bitwise Operators
Assignment Operators
Misc
Operators
Control Statements:
Branching
Looping
Input and Output:
printf() function
scanf() function
Pointing to Data:
Pointers and Arrays
Pointer
Arithmetic
Pointer Arithmetic
with arrays
Functions:
Using functions
Declaration and
Definition
Strings:
Reading and Writing
Strings
String Manipulation Function
Structured DataTypes:
Structure
Pointer to Structure
Working with Files:
Files
Basic I/O
Bits:
Bits Manipulation
Bits Field
Pre-Processors:
Pre-Processors Examples
Parameterized Macros
Macro
Caveats
Useful Concepts
Built-in Library Functions:
String Manipulation
Function
Memory Management
Function
Buffer
Manipulation
Character
Functions
Error Handling
Functions
Soft Skills
Communication Skills
Leadership Skills
.........More
|
|
C Programming Tutorials
Strings
-
In C language Strings are defined as an array of
characters or a pointer to a portion of memory containing ASCII characters.
A string in C is a sequence of zero or more characters followed by a NULL
'\0' character:
-
It is important to preserve the NULL terminating
character as it is how C defines and manages variable length strings. All
the C standard library functions require this for successful operation.
-
All the string handling functions are prototyped in:
string.h or stdio.h standard header file. So while using any string related
function, don't forget to include either stdio.h or string.h. May be your
compiler differs so please check before going ahead.
-
If you were to have an array of characters WITHOUT the
null character as the last element, you'd have an ordinary character array,
rather than a string constant.
-
String constants have double quote marks around them, and
can be assigned to char pointers as shown below. Alternatively, you can
assign a string constant to a char array - either with no size specified, or
you can specify a size, but don't forget to leave a space for the null
character!
char *string_1 = "Hello";
char string_2[] = "Hello";
char string_3[6] = "Hello"; |
Reading and Writing Strings:
One possible way to read in a string is by using scanf. However, the problem
with this, is that if you were to enter a string which contains one or more
spaces, scanf would finish reading when it reaches a space, or if return is
pressed. As a result, the string would get cut off. So we could use the gets
function
A gets takes just one argument - a char pointer, or the name of a char array,
but don't forget to declare the array / pointer variable first! What's more, is
that it automatically prints out a newline character, making the output a little
neater.
A puts function is similar to gets function in the way that it takes one
argument - a char pointer. This also automatically adds a newline character
after printing out the string. Sometimes this can be a disadvantage, so printf
could be used instead.
#include <stdio.h>
int main() {
char array1[50];
char *array2;
printf("Now enter another string less than 50");
printf(" characters with spaces: \n");
gets(array1);
printf("\nYou entered: ");
puts(array1);
printf("\nTry entering a string less than 50");
printf(" characters, with spaces: \n");
scanf("%s", array2);
printf("\nYou entered: %s\n", array2);
return 0;
} |
This will produce following result:
Now enter another string less than 50
characters with spaces:
hello world
You entered: hello world
Try entering a string less than 50 characters, with spaces:
hello world
You entered: hello |
String Manipulation Functions:
-
char *strcpy (char *dest, char *src);
Copy src string into dest string.
-
char *strncpy(char *string1, char *string2, int n);
Copy first n characters of string2 to stringl .
-
int strcmp(char *string1, char *string2);
Compare string1 and string2 to determine alphabetic order.
-
int strncmp(char *string1, char *string2, int n);
Compare first n characters of two strings.
-
int strlen(char *string);
Determine the length of a string.
-
char *strcat(char *dest, const char *src);
Concatenate string src to the string dest.
-
char *strncat(char *dest, const char *src, int n);
Concatenate n characters from string src to the string dest.
-
char *strchr(char *string, int c);
Find first occurrence of character c in string.
-
char *strrchr(char *string, int c);
Find last occurrence of character c in string.
-
char *strstr(char *string2, char string*1);
Find first occurrence of string string1 in string2.
-
char *strtok(char *s, const char *delim) ;
Parse the string s into tokens using delim as delimiter.
NEXT >> Structured DataTypes
Have a Question ?
post your questions here. It
will be answered as soon as possible.
Check
C Aptitude Questions
for more C Aptitude Interview Questions with Answers
Check
C Interview Questions
for more C Interview Questions with Answers
|