C Tutorial String Code example

Data types in C
Arrays in C
Pointers in C
Pointer Dereferencing
The & Operator
NULL
Uninitialized Pointers
Using Pointers
C Strings
String Code Example

Large Strings
Char type pointers
TypeDef



 
 
 

 

HOME
 

Here's a moderately complex for loop which reverses a string stored in a local array. It demonstrates calling the standard library functions strcpy() and strlen() and demonstrates that a string really is just an array of characters with a '\0' to mark the effective end of the string. Test your C knowledge of arrays and for loops by making a drawing of the

memory for this code and tracing through its execution to see how it works.


char string[1000]; // string is a local 1000 char array int len;

strcpy(string, "binky");
len = strlen(string);

/*
Reverse the chars in the string:
i starts at the beginning and goes up
j starts at the end and goes down

i/j
*/

exchange

their chars as they go until they meet

int i, j;
char temp;
for (i = 0, j = len - 1; i < j; i++, j--) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
}

// at this point the local string should be "yknib"

}

Want To Know more with

Video ???