473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

how to copy a pointer string into a normal string which is of type "char"

please help me out in this regard as this is urgent to me because i stuck in the middle of a program.......
here I have a program like this......


char name[]="rajesh";
char *x=strchr(name,"j");
int k =strlen(name);
int i=x-name;
int p=k-i;


here I want to copy the string "jesh" into name[]..because i want 2 make it in loop....


so i have written like this...

name=&x[p];

I know here the name[] is not of pointer type so it is not working...

please help me....
and I have one more doubt here. can we write strchr(name,'s'); where name is of pointer type....
Jul 22 '08 #1
4 2364
gpraghuram
1,275 Expert 1GB
You can achive the same like this

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     char arr[] = "abcdef";
  4.     printf("%s\n",arr);
  5.     sprintf(arr,"%s",(arr+2));
  6.     printf("%s\n",arr);
  7. }
  8.  
  9.  
You can use this logic with strchr to determine the start position

Raghu
Jul 22 '08 #2
ashitpro
542 Expert 512MB
First of all...
I'd like to remind you that 'name' is character array...You can not change base address of any array.
So "name=&x[p];" this statement is invalid...
If name was character pointer then it would have worked.
If you don't want it as character pointer..simply use for loop and copy the desired string.

Regarding your second question...you can have character pointer as your first arg to strchr..provided that it is null terminated....
Jul 22 '08 #3
newb16
687 512MB
here I want to copy the string "jesh" into name[]..because i want 2 make it in loop....
I don't get it. Do you want to use two loops?
As x points to character within name, and x itself is a string, ewe can't use strcpy(name,x); Use memmove() instead as it's guaranteed to work with overlapped areas.


and I have one more doubt here. can we write strchr(name,'s'); where name is of pointer type....
Yes.
Jul 22 '08 #4
Iam very much thankful to all who helped me in solving this problem.....


I did it using "for loop" only....


Here I came to know one new thing that is while using pointer in "strchr" that should be terminated with "null" character...

Thanks alot......the second one helped me alot in writing my program......
Jul 22 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Vaca Louca | last post by:
Hello, I write an ISAPI authentication module which uses Berkeley DB and want it to be as efficient as possible. Both ISAPI and BerkeleyDB use arrays of chars (char *) to pass and receive...
2
by: srktnc | last post by:
When I run the program, I get a Debug Error saying "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more...
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
34
by: arnuld | last post by:
what is the difference between these 2: char name = "hackers"; char* name = "hackers";
20
by: liujiaping | last post by:
I'm confused about the program below: int main(int argc, char* argv) { char str1 = "abc"; char str2 = "abc"; const char str3 = "abc"; const char str4 = "abc"; const char* str5 = "abc";
3
by: Mahesh | last post by:
#include <stdio.h> #include <string.h> #include <malloc.h> int main(void) { int key = 120; char *s = "String to Encrypt using XOR"; char *ss = s; char echar; char dchar;
13
by: Anna | last post by:
I try to put 8 int bit for example 10100010 into one character of type char(1 octet) with no hope . Could anyone propose a simple way to do it? Thank you very much.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.