Question

In: Computer Science

C Program: Write your own stringLength function (without calling a pre-written library function) that returns the...

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.

Solutions

Expert Solution

#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;
}


Related Solutions

in c++, without using any built-in random library already exist, instead,write your own function to generate...
in c++, without using any built-in random library already exist, instead,write your own function to generate random char from a list of: 10 letters T 10 letters H 10 letters A 10 letters N 10 letters K
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x...
Problem: Write a C/C++ program to implement a function that returns 1 when an integer x contains an odd number of 1s (in its binary representation) and 0 otherwise. You can consider x as a four byte integer, i.e. w = 32 bits. Constraint: Your solution can use at most 12 arithmetic, bitwise, and logical operations.
Please, write this code in c++. Using iostream and cstring library. Write a function that will...
Please, write this code in c++. Using iostream and cstring library. Write a function that will delete all words in the given text that meets more that one time. Also note than WORD is sequence of letters sepereated by whitespace. Note. The program have to use pointer. Input: First line contains one line that is not longer than 1000 symbols with whitespaces. Each word is not longer than 30 symbols. Output: Formatted text. example: input: Can you can the can...
This is a beginner C++ class Your program will be creating 10 library books. Each library...
This is a beginner C++ class Your program will be creating 10 library books. Each library book will have a title, author and publishing year. Each library book will be stored as a structure and your program will have an array of library books (an array of structures). After creating the struct, the next task is to create a new separate array for book genres. You will create this corresponding array of book genres: Mystery, Romance, Fantasy, Technology, Children, etc....
Create your own function in C that accepts one input number and returns a double number....
Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following: Calculates 3.14 times of the square of input number. For example, if 2 is input then 12.56 should be returned. (e.g. 3.14*2*2 = 12.56)
Create your own function in C that accepts one input number and returns a double number....
Create your own function in C that accepts one input number and returns a double number. The themes for the functions should be one of the following: Divides the number by 3 and returns the result. For example, if 6 was input then 2.0 should be returned. provide both your C code and an example call to the C code function. Be sure to provide an overview of what your function is doing. Include header documentation in the code as...
Using c++, write a program that will display your name as a void function then will...
Using c++, write a program that will display your name as a void function then will perform the following by user-defined functions: a. to compute for the sum of two numbers (n1, n2) using function.
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library...
Programming Language Required: C Write a multithreaded program in C (not c++) using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally,...
Please, write code in c++. Using iostream and cstring library Write a function that will find...
Please, write code in c++. Using iostream and cstring library Write a function that will find and return most recent word in the given text. The prototype of the function have to be the following void mostRecent(char *text,char *word) In char *word your function have to return the most recent word that occurce in the text. Your program have to be not case-sensitive(ignore case - "Can" and "CAN" are the same words) Also note than WORD is sequence of letters...
THIS PROGRAM HAS TO BE WRITTEN IN C Single function to perform the parity computation on...
THIS PROGRAM HAS TO BE WRITTEN IN C Single function to perform the parity computation on the input integer passed to this function. The output of the function is 0 if the input has even parity (that is, the number of 1s in the binary representation of the input is even) and 1 if the input has odd parity (that is, the number of 1s in the binary representation of the input is odd).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT