views:

241

answers:

6

i need to create a small program: user enter 1st string ex: I'm a beautiful girl then he needs to replace for example 'beautiful' to 'clever'

can use just stdio.h and pointers as well

help me, pls!!!

+1  A: 

You should probably take a look at the fgets function of stdio.h

James
`fgets()` is a function, not method. There are no methods in C.
qrdl
@qrdl same thing but different name. correct me if i'm wrong.
starcorn
@klw: Functions are generally considered standalone, atomic. Methods are considered part of a class. There is a whole SO discussion on this topic: http://stackoverflow.com/questions/43777/method-vs-message-vs-function-vs
0A0D
@Changeling: i'll have a look in that, thanks for informing
starcorn
+1  A: 

Here is the outline of an algorithm

1. Parse arguments search-string and replacement-string
2. Read input line
3. Search for search-string in input line and replace it by replacement-string
4. Output line

Step 3 will have to use handcrafted loops for string operations because the typically used functions are found in <string.h>

Peter G.
Binary Worrier
@Binary Worrier Thanks, it always pays to read the assignment carefully. Corrected.
Peter G.
+3  A: 

First, write the code and use functions in string.h as usual. Now you know which functions you need to replace.

Then, research and/or ask specific questions to do with replacing those functions.

Hope this helps.

Binary Worrier
A: 
#include <stdio.h>
#define SIZE 1000

void read_str();


main() {
    char search_key;
    char *ptr1;

    char s1[SIZE];

    ptr1 = s1;


    printf("Enter your string:\n");
    read_str();

    printf("Enter the key word u want to change:");
    scanf("%c", &search_key);
    // after this i don't have idea what to do....((((((

    return 0;
}

void read_str()
{
    int c;
    while((c = getc(stdin))!= '\n')
    *ptr1++ = c;
    *ptr1++ = ' ';
    *ptr1 = '\0';
}
Jeniffer
A: 

Not sure about C but I did something similar in C++. C might have a similar range of string manipulations. userInput="I am a beautiful girl" replaceString=userInput.replace(5,9,"Clever");

svenus
no it has not, and moreover the OP is forced to use string.h or anything else than stdio.h ...
ShinTakezou
+1  A: 

First, let us suppose you want just literal substitution and not something more complicated that would distinguish from "beautifulgirl" and "beautiful girl".

First, write your compare function.

  • Inputs: ptr to buffer (buf), word to find (w)
  • (1) while ( *w != '\0')
  • if *buf != *w return 0 (false)
  • buf++, w++ and loop (1)
  • return 1 (true)

Then scan your input buf and copy it to dbuf (they must be big enough);

  • i = 0; j = 0;
  • (1) if ( buf[i] == '\0' ) dbuf[j] = 0; exit loop
  • if compare(&buf[i], "beautiful") is 1 (true), copy "clever" to &dbuf[j], then skip "beautiful" (update i) and update j (for "clever") and loop (1)
  • dbuf[j] = buf[i]; i++; j++; loop (1)

This will change every instance of "beautiful", being a word or not.

To skip beautiful, it is enough to increment i by the length of beautiful; the dest buffer index j must be instead incremented by the length of "clever". To avoid hardencoding lengths, the following

int strlen(char *s)
{
  char *p = s;
  while( *s != '\0' ) s++;
  return s-p;
}

should be a possible impl of strlen (change its name eventually).

Bugfixes and real code implementation are left to you (since the task is very boring indeed)

ShinTakezou
than q, so much!!!!i'll try to try it now)
Jeniffer