How can I do that ?


 
Thread Tools Search this Thread
Top Forums Programming How can I do that ?
# 1  
Old 11-04-2006
How can I do that ?

I have a problem.

I have a string , let's say a="!_30".
I have another string to , let's say b="cool++".
I wanna replace string a value with cool.
I wanna a="cool" , without those 2 pluses.

How can I do it , with some C string function. Not vith array's or pointer's ?

I mean , I want , a be cool , in place of !_30.

I made it to replace totaly a with b , but I wanna replace totaly a with only one portion of b .. Smilie
# 2  
Old 11-04-2006
Try this:
Code:
$ cat s.c
#include <stdio.h>
#include <string.h>
main()
{
        char a[]="!_30";
        char b[]="cool++";
        printf("a = %s \n", a);
        strncpy(a,b,strlen(a));
        printf("a = %s \n", a);
        exit(0);
}
$ gcc s.c -o s
$ ./s
a = !_30
a = cool

# 3  
Old 11-05-2006
Hey , dude , I try that code out .. but this is not what I wanted.I wanted string a to be totaly replaced with string b without those 2 pluses.

Let's take an example , on your code :

Code:
#include <stdio.h>
#include <string.h>
main()
{
        char a[]="not working";
        char b[]="cool++";
        printf("a = %s \n", a);
        strncpy(a,b,strlen(b)-2);
        printf("a = %s \n", a);
        printf("b = %s \n",b);
        exit(0);
}

result :
a = not working
a = coolworking
b = cool++

The thing is , that I wanted , a become a="cool" , not a become "coolworking" .. totaly replace.In my first example strlen(!_30)==strlen(cool) .. my bad ..

Smilie

P.S. : I hate C , on string processing .. Smilie


I made something , like , and it's working like I want ( maybe some users need this idea ).

Code:

#include <stdio.h>
#include <string.h>

int
main()
{

char a[]="now is working";
char b[]="cool++";
char bug[256];
int g;

printf("a = %s \n",a);

        for (g=0;g<strlen(b)-2;g++)
                      { bug[g]=b[g]; }
                       bug[g]='\0';
        stpcpy(a,bug);
        printf("a = %s \n",a);
exit(0);


        }

Result :

a="now is working"
a="cool".

Smilie

Last edited by !_30; 11-05-2006 at 06:17 AM..
# 4  
Old 11-05-2006
!_30, Using your previous example, would it work to just null the string before you copy anything into it?

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
        char a[]="not working";
        char b[]="cool++";
        printf("a = %s \n", a);
        memset( a , '\0' , strlen(a) );
        strncpy(a,b,strlen(b)-2);
        printf("a = %s \n", a);
        printf("b = %s \n",b);
        exit(0);
}
$ a.out
a = not working
a = cool
b = cool++

# 5  
Old 11-12-2006
Ya , it's easier..

I have a problem.

Let's say I have this string s="send !_30 <what I wanna send>".

It's there a C fucntion ( in strings ) , I can use to copy !_30 in another string.
I mean , a C function to copy a string beginning from position x and ending in position y from string s.

Let's say : s="send !_30 <what I wanna send>"
012345678

something like this : copy from position 5 to postion 8 , to string s1 meaning s1="!_30"


By the way : Does it exist a function , I can use to see if 2 C string's are identical ? Smilie

P.S. : I made it to copy that string ... but I'm still curios if I can do it easier with a function ... Smilie
Smilie

Last edited by !_30; 11-12-2006 at 10:32 AM..
# 6  
Old 11-12-2006
You have two questions:
1. can you copy given number of chars from one string to another - starting from a position chosen by you
2. can you compare two strings

Code:
#include<stdio.h>
#include<string.h>

int main() {

        char s[]="send !_30 <what I wanna send>";
        char s1[10];
        char s2[]="tesT";
        char s3[]="tesT";
        
        /* strncpy to copy strings */
        strncpy(s1,s+5,4);
        fprintf(stdout,"%s\n",s1);
        
        if(strcmp(s2,s3)==0) {
                fprintf(stdout,"strings are identical...\n");
        }
}

Note that you cannot specify directly to the function the starting position of the string. You have to use something like s+5 to move to the start position of the copy.
# 7  
Old 11-13-2006
Thank's dude ! This is the thing I wanted . Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question