/***************************************************************************************
Develop a program that allows the user to enter a string and performing the following operations on it:
· Count Number of Characters In String
· Count Number of Words In String
· Remove Spaces In String
· Reverse String
· Reverse Words In String
· Look For Substring In String
· Insert Substring Into String
If option 1 is chosen, 7 (including any special characters but excluding spaces) is shown on the screen.
If option 2 is chosen, 3 is shown on the screen
If option 3 is chosen, 'CisFun!' is shown on the screen
If option 4 is chosen, '!nuF si C' is shown on the screen
If option 5 is chosen, 'C si !nunF' is shown on the screen
If option 6 is chosen, get user to enter the substring he wants to find and display a message to tell him whether it is in the string
If option 7 is chosen, get user to enter a substring, and the position he wants to insert into the original string. The new string is then displayed on the screen.
Note:
You should develop test cases (of screen captures) to show the successful running of the program.
**************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
/* String Manipulation Function With Out using Any C libraries, stdlib.h is used for only exit(0) function */
/* All Function Prototype That is Used for Calling Every user choice */
void display(void);
int count_excluding_space(char *str);
int count_string(char *str);
char *remove_space(char *str, char *string);
char *reverse_string(char *original, char *reverseStr);
void reverse_word(char * word);
char *Find_Substring(char *String, char *substring);
void insert_substring(char * str, char * substr, int pos);
void Exit(void);
int main()
{
char str[80];
char string[80];
int choice;
printf("Enter line of text:\n");
scanf("%80[^\n]", str);
display();
printf("\nEnter Your choice: ");
fflush(stdin);
scanf("%d", &choice); /* Asking User Which Function Would they like to View */
do
{
switch(choice)
{
case 1:
printf("\n\nNumber Of Characters In String =%d\n",
count_excluding_space(str));
break;
case 2:
printf("\nThe Numbers Of Words In String = %d\n",
count_string(str));
break;
case 3:
printf("\nAfter Removing Spaces In String \t\n%s\n",
remove_space(str, string));
break;
case 4:
printf("\nAfter Reversing String It Looks like as: \n%s\n",
reverse_string(str, string));
break;
case 5:
reverse_word(str);
break;
case 6:
{
printf("Enter Sub-String That You Want to find: ");
fflush(stdin);
scanf("%20[^\n]", string);
char * sub = Find_Substring(str, string);
if (sub != NULL)
printf("The substring\"%s\" is in \"%s\" \n", string, str);
else
printf("The substring\"%s\" is not in \"%s\" \n",string, str);
}
break;
case 7:
{
int pos =0;
printf("Enter a substring to insert, upto 20 chars\n");
fflush(stdin);
scanf("%20[^\n]", string);
fflush(stdin);
printf("Position to insert: ");
scanf("%d", &pos);
insert_substring(str, string, pos);
break;
}
case 8:
Exit();
break;
default:
printf("Invalid Choice: (Note: 8 gives you rest)\n\n");
break;
}
printf("Enter your choice: ");
fflush(stdin);
scanf("%d", &choice);
}while(choice!=8);
return 0;
}
/* Function Which Display the Information of Every selection of number */
void display(void)
{
fflush(stdin);
printf("\n\n1. Count Number of character excluding Space\n");
printf("2. Count Number of words on string\n");
printf("3. Remove Spaces in String\n");
printf("4. Reverse String\n");
printf("5. Reverse Word\n");
printf("6. Search Substring\n");
printf("7. Insert a substring\n");
printf("8. Exit \n");
}
/* Function to return the number of word in Entire String. */
int count_excluding_space(char *str)
{
int count =0;
int i= 0;
while(str[i]!='\0')
{
if(str[i]!=' ')
{
count++;
}
i++;
}
return count;}
/* Function For Counting the String on Given Sentences */
int count_string(char *str)
{
int count=0, i=0;
int charfound =0;
while(str[count]!='\0')
{
if ((str[count] != ' ') && (str[count]!= '\t'))
{
if (charfound==0)
{
i++;
charfound =1;
}
}
else
{
charfound=0;
}
count++;
}
return i;
}
/* Function For displaying all cahracters without soace. */
char *remove_space(char *str, char *converted)
{
int i =0;
int count =0;
while(str[i] !='\0')
{
if (str[i] != ' ')
{
converted[count]=str[i];
count++;
}
i++;
}
converted[count] = '\0';
return converted;
}
/* Function for Reversing String within given sentences. */
char *reverse_string(char *original, char *reverseStr)
{
int i = 0;
int count =0;
while(original[i]!= '\0')
{
i++;
}
i--;
while(i >= 0)
{
reverseStr[count] = original[i];
count++;
i--;
}
reverseStr[count]='\0';
return reverseStr;
}
/* Function for Reversing Words from the string */
void reverse_word(char *reverseWord)
{
int i, j, k=0;
char temp1[80], temp2[80];
for(i =0; reverseWord[i]!= '\0'; i++) ;
/* Where i is iniliased by the total length of the string */
for(j =0; j <= i; j++)
{
k =0;
/* Skip the White Space */
while ((reverseWord[j] == ' ') || (reverseWord[j]=='\t'))
{
j++;
}
while((reverseWord[j]!= ' ') && (reverseWord[j]!='\t') && (reverseWord[j]!='\0'))
{
temp1[k++] = reverseWord[j++];
}
temp1[k] = '\0';
reverse_string(temp1, temp2);
printf("%s ", temp2);
}
printf("\n");
}
//=============================================================
// Finds the substring str2 into the string str1.
// if the sub-string is found, it returns the pointer of the
// address where the substring begins. It behaves like the strstr
// function of string.h library.
//================================================================
char *Find_Substring( char * str1, char * str2 )
{
char *cp = (char *) str1;
char *s1, *s2;
if ( !*str2 )
return((char *)str1);
while (*cp)
{
s1 = cp;
s2 = (char *) str2;
while ( *s1 && *s2 && !(*s1-*s2) )
s1++, s2++;
if (!*s2)
return(cp);
cp++;
}
return(NULL);
}
/* Function That find out the substring in orginal string. And after searching prints the result */
void insert_substring(char * str, char * substr, int pos){
char new_string[100] = ""; /* initialize to NULL */
int i =0;
int j =0;
for (i =0; i < pos; i++){
//copy from the old string
new_string[i] = str[i];
}
// now copy the substring to the new string.
while ( substr[j] != '\0') {
new_string[i] = substr[j];
i++;
j++;
}
// Now copy the remaining of the original string to the
// new string.
for (j =pos; str[j] != '\0'; j++, i++) {
new_string[i] = str[j];
}
printf("The new string after inserting the substring is\n%s\n",
new_string);
}
/* FUnction to exit the Whole Running programs and returns to the coding */
void Exit(void)
{
fflush(stdin);
printf("\nThank You . . . . HAVE A NICE DAY ! ! !");
printf("\nPress Enter Key To return To Program. ...\n");
fflush(stdin);
getchar();
exit(0);
}
E-mail: homananda
Clcik Below to see it on Big Window
You are the  : |
|
Persons to see this page since May 2002 |
![]() |