answersLogoWhite

0


Best Answer

What U exactly want?

Here I am giving "complete c program for string manipulation using pointer "

/*********************************PRACT.*********************************
Title:: STRING MANIPULATION(functions implemented with pointers).
Operations covered :1:Accept 2:Display 3:String Length 4:String Copy 5:String Reverse
------------------- 6:Check for Palindrome 7:String Comparison
8:Substring(String Searching) 9:StrConcatinate
*************************************************************************/
#include
#include
/*
I Am Just Giving FUNCTION DEFINATION 1'st & then "main function.
*/

//Fun.str
void str()
{ int i;
printf("\n\n");
for(i=0;i<30;i++)
printf("*~");
}
//Fun.1 for Accepting string.
void Accept(char *a)
{ printf("\nEnter The String : ");
scanf("%s",a);
}
//Fun.2 Displaying string which is accepted in Fun.1
void Disp(char *a)
{ printf("\n\tString =%s",a);
}

//Fun.3 Displaying length of string.
int len(char *a)
{ char *p;
int count=0;
for(p=a;*p!='\0';p++)
count++; //incrementing count till for loop exists
return(count);
}

//Fun.4 To copy string a/b.
void copy(char *a,char *r)
{ while(*a!='\0')
{ *r=*a; //coping the string character by character.
r++;a++;
}
*r='\0';
}

//Fun.5 To find out reverse of string a/b.
void reverse(char *a)
{ char *p,*q;
int count=0;
for(p=a;*p!='\0';p++)
count++;
while(*p!='\0')
p++;
while(count>0)
{ p--;
count--;
printf("%c",*p);
}
}

//Fun.6 Check for palindrome
int pl(char *a)
{ char *p,*q;
p=q=a;
while(*q!='\0')
q++; // q represents the null character.
q--;//q represents the last character of string.
while(p{ if(*p!=*q)
return(0);
p++;q--;
}
return(1);
}

//Fun.7 comparing 2 strings .
int compare(char *a,char *b)
{ while(*a!='\0')
{ if(*a==*b)
return(1);
a++;b++;
}
return(0);
}

//Fun.8 searching a string
int Substr(char *a ,char *b)
{ int N1,N2,count=0;
char *p,*q,*k;
for(N1=0;*(a+N1)!='\0';N1++); //N1=length of string1
for(N2=0;*(b+N2)!='\0';N2++); //N2=length of string2

for(p=a;p<=a+N1-N2+1;p++)
{ k=p;
for(q=b;*k==*q&& *q!='\0';q++,k++); // string b[] is matched in string a[] from the position p.
if(*q=='\0')
{ count++;
printf("\nLOCATION = %d",p-a+1);
}
} return(count);
}

//Fun.9 String concatination.
void StrCon(char *a,char *b)
{ char *p,*q;
while(*a!='\0')
a++;
while(*b!='\0')
{ *a=*b;
a++; b++;
}
*a='\0';
}


/**************** "main function " ****************************/
void main()
{ char a[25],b[25],c[25],s;
int k,l;
clrscr();
do
{str();
printf("\n\t\tSTRING MANIPULATION WITH POINTER.");
str();
printf(" \n\t1:Accept \n\t2:Display \n\t3:String Length \n\t4:String Copy \n\t5:String Reverse \n\t6:Check for Palindrome \n\t7:String Comparison\n\t8:Substring(String Searching) \n\t9:StrConcatinate \n\t10:Exit.");
printf("\nEnter The Choice..... . . . . " );
scanf("%d",&k);
switch(k)
{ case 1:Accept(a);
break;
case 2:Disp(a);
break;
case 3:l=len(a);
printf("Length of string %s = %d",a,l);
break;
case 4:copy(a,c);
printf("Copied String = %s",c);
break;
case 5:reverse(a);
break;
case 6:l=pl(a);
if(l==0)
printf("\n%s : NOT a PALINDROME.",a);
else
printf("\n%s is PALINDROME.",a);
break;
case 7:Accept(b);
l=compare(a,b);
if(l==1)
printf("\nStrings (%s & %s )are SAME",a,b);
else
printf("\nStrings (%s & %s )are Different",a,b);
break;
case 8:Accept(b);
l=Substr(a,b);
if(l>=1)
printf("\n%s is SubString of %s \nOccurunce : %d times.",b,a,l);
else
printf("\n%s is NOT a SubString of %s .",b,a);
break;
case 9:Accept(b);
StrCon(a,b);
printf("Concatinated string = %s ",a);
break;
case 10:exit();
default:printf("\nPlease enter Proper Value. ");
}
printf("\nDo U want to Continue(y/n)? ");
scanf("%s",&s);
}while(s=='y's=='Y');
if(s=='n')
exit();
getch();
}

/* Hope it is helpful */

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: C program for string replacing function using pointers?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

How do you use bsearch function to search a name sorted in array of pointers to string?

solution of above question on (link moved to link section)


How are the strings passed to a function?

By reference. The name of the string is converted to a pointer (in C/C++) and given to the function as the address of the first element. (In Java, all objects are passed by reference, and there are no pointers.)


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


What is a pointer in C programming language?

A pointer in C is the address of a variable or function.The advantages of a pointer over using normal variables are:If you pass the pointer to a variable to a function, then that function can modify the value of the variable directly; for example, suppose you want a function to convert a string to lower case. If pointers are not available, then a function call would look like this (in BASIC):NewString = ToLower("This Is A String")What the BASIC compiler does is make a copy of the "This Is A String" string, pass it to the ToLower() function and return a copy back to the caller to be copied to NewString.All these copies make the process slow. In C you can do this:StringPtr = "This Is A String"ToLower(StringPtr)StringPtr points to the string, ToLower() receives the pointer and performs the conversion on the original string without any copying taking place.Function pointers allow C to be used in a more object orientated way; C doesn't support objects, but a structure can contain a collection of data and function pointers so the structure can effectively contain data and the functions needed to process it.The danger of pointers is that they point directly into computer memory and its impossible for the compiler to be certain that you haven't made a pointer point outside of the program memory; the way you find out is when the program crashes when run with an exception error.There is a also slight format error with the way pointers are described in C; for example consider the following function:int Divide(int *a, int *b){int r;r = *a/*b;return r;}Although it looks harmless, the compiler can't tell if the /*b; is introducing a comment or is part of the calculation and most compilers will produce a missing close comment message - although some will silently compile the program - but commenting out everything after the *a!Read more on C from : http://www.indiabix.com/c-programming/index.phpRead more on C Pointer from :http://www.indiabix.com/c-programming/question-answer.php?topic=nqvqmuprt


How do write a function that receives two pointers to character strings where the function concatenates the two strings and prints the new concatenated string?

#include &lt;iostream&gt; #include &lt;string&gt; std::string* concat_print_strings(std::string* pStr1, std::string* pStr2 ) { std::string * strResult = new std::string( *pStr1 ); strResult-&gt;append( *pStr2 ); std::cout &lt;&lt; strResult-&gt;c_str() &lt;&lt; std::endl; return( strResult ); } int main() { std::string str1 = "This is a string."; std::string str2 = " And this is another string."; std::string* pStr = concat_print_strings( &amp;str1, &amp;str2 ); delete( pStr ); pStr = NULL; return( 0 ); }

Related questions

How do you use bsearch function to search a name sorted in array of pointers to string?

solution of above question on (link moved to link section)


How are the strings passed to a function?

By reference. The name of the string is converted to a pointer (in C/C++) and given to the function as the address of the first element. (In Java, all objects are passed by reference, and there are no pointers.)


Write a Program to convert decimal number into hexadecimal no without using function and string?

This is not a question.


What are examples of a palindrome program forward and backwards?

To check if a string is a palindrome, point to each end of the string and work inwards towards the middle. If the characters pointed at differ, the string is not a palindrome. When the pointers meet or cross each other, the string is a palindrome. Note that the string cannot contain whitespace or punctuation and comparisons must not be case-sensitive.


Piglatin string in c without using pointer?

Sure, you can write a function in C to convert a string to Pig Latin without using pointers by passing the string as a parameter and manipulating it directly within the function. You can split the string into words, check if a word starts with a vowel or consonant, and then apply the appropriate transformation following the rules of Pig Latin. Remember to allocate enough memory for the modified string to prevent buffer overflow.


What is a pointer in C programming language?

A pointer in C is the address of a variable or function.The advantages of a pointer over using normal variables are:If you pass the pointer to a variable to a function, then that function can modify the value of the variable directly; for example, suppose you want a function to convert a string to lower case. If pointers are not available, then a function call would look like this (in BASIC):NewString = ToLower("This Is A String")What the BASIC compiler does is make a copy of the "This Is A String" string, pass it to the ToLower() function and return a copy back to the caller to be copied to NewString.All these copies make the process slow. In C you can do this:StringPtr = "This Is A String"ToLower(StringPtr)StringPtr points to the string, ToLower() receives the pointer and performs the conversion on the original string without any copying taking place.Function pointers allow C to be used in a more object orientated way; C doesn't support objects, but a structure can contain a collection of data and function pointers so the structure can effectively contain data and the functions needed to process it.The danger of pointers is that they point directly into computer memory and its impossible for the compiler to be certain that you haven't made a pointer point outside of the program memory; the way you find out is when the program crashes when run with an exception error.There is a also slight format error with the way pointers are described in C; for example consider the following function:int Divide(int *a, int *b){int r;r = *a/*b;return r;}Although it looks harmless, the compiler can't tell if the /*b; is introducing a comment or is part of the calculation and most compilers will produce a missing close comment message - although some will silently compile the program - but commenting out everything after the *a!Read more on C from : http://www.indiabix.com/c-programming/index.phpRead more on C Pointer from :http://www.indiabix.com/c-programming/question-answer.php?topic=nqvqmuprt


Geta is a math function or string function?

GetA is a math function and not a string function.


How do write a function that receives two pointers to character strings where the function concatenates the two strings and prints the new concatenated string?

#include &lt;iostream&gt; #include &lt;string&gt; std::string* concat_print_strings(std::string* pStr1, std::string* pStr2 ) { std::string * strResult = new std::string( *pStr1 ); strResult-&gt;append( *pStr2 ); std::cout &lt;&lt; strResult-&gt;c_str() &lt;&lt; std::endl; return( strResult ); } int main() { std::string str1 = "This is a string."; std::string str2 = " And this is another string."; std::string* pStr = concat_print_strings( &amp;str1, &amp;str2 ); delete( pStr ); pStr = NULL; return( 0 ); }


When do you use pointers in C?

There are many uses of pointer in C. Pointers are efficient and elegant to use. All string variables are pointers, and calling a function using a pointer allows the function to change the value globally. These are what you can do with pointers in C. 1) Returning multiple values by passing address of variables. eg. foo(&amp;a,&amp;b,&amp;c); 2) When you want to modify the value passed to function. eg. scanf() function. int n; scanf("%d",&amp;n); /* pass address of variable n so that scanf can change its value*/ 3) When you need to pass large data structure to function, it is more efficient to pass pointer to structure than passing the entire structure. If you don't want to modify the structure members, you can use 'const' keyword which the ANSI-complaint C compiler will either warn or give error if you modify. eg strlen(const char *str) in string library should not modify the content of str. 4) Implementing 'goto' data structures are easy with pointers. eg. linked-list,binary trees, hash table. 5) You can allocate dynamic memory using pointers. 6) Pointers to function are used for call back function and jump table. eg qsort() and bsearch() functions require the caller to provide pointer to function to do the comparison.


Write a program to concate the string without using strconcat function?

If you don't need to preserve the first string you could just iterate over the second string and copy each character onto the end of the first string, then return that


Which is string function starchy or strlength?

The string function is strlength and it is evoked to return the length of a string.


How do you make a program to reverse nummber in c language?

I'd use sprintf (assuming the number wasn't a string already) and pointers. If that's not enough of a clue, you're really not ready to be in this programming class.