In: Computer Science
C Program:
Write your own stringLength function (without calling a pre-written library function) that returns the length of the string argument in characters, not including the terminating ‘\0’ character.
Write four versions of a void printString function (i.e.,
printString1, printString2, etc) each of which prints its string
argument character-by-character but using the following
techniques:
printString1: array indexing
printString2: pointer/offset with the array name as a
pointer,
printString3: pointer indexing, and
printString4: pointer/offset with a pointer
Write a void function called swapChar that swaps the values stored in two char variables.
Write a void reverseString function that accepts a single string parameter and reverses the characters in the string using calls to your stringLength and swapChar functions.
Write a main function which:
1) declares an array of characters large enough to hold 20
characters plus the terminating null character,
2) prompts the user for an input string no longer than 20
characters,
3) calls each of the 4 printString functions passing the input
string as the argument,
4) calls reverseString passing the input string as the
argument,
5) calls each of the 4 printString functions passing the (reversed)
input string as the argument.
#include<stdio.h>
//function that returns the length of the
//char array passed.
int stringLength(char str[])
{
int len =0;
while(str[len] !='\0')
{
len++;
}
return len;
}
//function that prints the
//char array passed by using array indexing.
void printString1(char str[])
{
printf("\nPrinting String Type 1\n");
int len = stringLength(str);
int i;
for(i =0;i<len;i++)
{
printf("%c",str[i]);
}
printf("\n");
}
//function that prints the
//char array passed by using pointer/offset with the array name as
pointer.
void printString2(char str[])
{
printf("\nPrinting String Type 2\n");
int len = stringLength(str);
int i;
for(i =0;i<len;i++)
{
printf("%c",*(str + i));
}
printf("\n");
}
//function that prints the
//char array passed by using pointer indexing.
void printString3(char str[])
{
printf("\nPrinting String Type 3\n");
int len = stringLength(str);
int i;
for(i =0;i<len;i++)
{
printf("%c",i[str]);
}
printf("\n");
}
//function that prints the
//char array passed by using pointer/offset with a pointer..
void printString4(char str[])
{
printf("\nPrinting String Type 4\n");
int len = stringLength(str);
int i;
for(i =0;i<len;i++)
{
printf("%c",*(i+str));
}
printf("\n");
}
//function that takes address of two chars
//and swaps them.
void swapChar(char *one,char *two)
{
char c = *one;
*one = *two;
*two = c;
}
//function that reverses the given char array.
void reverseString(char str[])
{
int len = stringLength(str);
int i =0;
//swap the i_th char and len-i-1 char
//each time till i<len/2;
for(i =0;i<=len/2;i++)
{
swapChar(&str[i],&str[len-i-1]);
}
}
int main()
{
char str[21];
//prompt user for input and store it.
printf("Enter string: ");
gets(str);
if(stringLength(str)>20)
{
printf("Input string must be less
than 20 chars");
return 0;
}
//call print versions
printString1(str);
printString2(str);
printString3(str);
printString4(str);
//reverse the string
reverseString(str);
printString1(str);
printString2(str);
printString3(str);
printString4(str);
return 0;
}