473,408 Members | 1,747 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,408 software developers and data experts.

More problems with string manipulation and seg faults

20
Alright, here's what I'm working on now. It's supposed to go through an inputted list of words, pick out actual words and create a numbered list of them, then, where it finds a number in the list, go back in the list from that number, replace it with a word, augment the list accordingly and continue.

Anyways, here's what I have:
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <ctype.h>
  4. #include <stdlib.h>
  5.  
  6. int main ()
  7. {
  8.     char line[100];
  9.     char word[100];
  10.     char tword[20];
  11.  
  12.     int k, index, nwords = 0, j;    
  13.  
  14.     gets(line);
  15.  
  16.     while(line[0] != '0')
  17.     {
  18.         if(isalpha(line[k]))     
  19.         {
  20.             j = 0;
  21.             while (isalpha(line[k]))
  22.             {
  23.                 tword[j] = line[k];
  24.                 j++;
  25.                 k++;
  26.             }
  27.             tword[j] = '\0';
  28.             strcpy(word[nwords], tword);
  29.             nwords++;
  30.         }
  31.         else if(isdigit(line[k]))
  32.         {
  33.             index = atoi(line[k]);
  34.             k++;
  35.             if(isdigit(line[k]))
  36.             {
  37.                 index = index * 10  + atoi(line[k]);
  38.                 k++;
  39.             }
  40.             printf("%s", word[nwords - index]);
  41.             strcpy(tword, word[nwords-index]);
  42.             for(j  = index + 1; j < nwords; j++)
  43.             {
  44.                 strcpy(word[j - 1], word[j]);
  45.             }
  46.             strcpy(word[nwords - 1], tword);
  47.         }
  48.         else
  49.         {
  50.             printf("%c", line[k]);
  51.             k++;
  52.         }
  53.     gets(line);    
  54.     }
  55.     return 0;
  56. }
The problem is that I get segmentation faults when executing. Anybody see what I'm missing?
Mar 7 '07 #1
3 1558
Banfa
9,065 Expert Mod 8TB
k is never initialised
Mar 8 '07 #2
Acolyte
20
Yeah, I noticed that right after I posted. However, I've changed that now, to no avail.
Mar 8 '07 #3
horace1
1,510 Expert 1GB
an obvious problem is line 28
Expand|Select|Wrap|Line Numbers
  1.             strcpy(word[nwords], tword);
  2.  
word[] is an array of char, it should be an array of char* or char[], e.g.
Expand|Select|Wrap|Line Numbers
  1.     char word[100][20];
  2.  
Mar 8 '07 #4

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

Similar topics

2
by: John Altland | last post by:
Hello, I need to access two databases on two seperate servers in one program. One, dblocal, and another, dbserver. Once I am done the queries on the local database, I dispose the connection...
12
by: Hp | last post by:
Hi All, Thanks a lot for all your replies. My requirement is as follows: I need to read a text file, eliminate certain special characters(like ! , - = + ), and then convert it to lower case and...
4
by: tbatwork828 | last post by:
Related to my other post on Graphics.FillRectangle and a lot of page faults caused by this call... We determine that when Control.DoubleBuffer=true to avoid the flicker effect,...
2
by: John Mullin | last post by:
We are having a problem which appears similar to a previous posting: http://groups.google.com/groups?hl=en&lr=&frame=right&th=d97f552e10f8c94c&seekm=OZw33z9EDHA.2312%40TK2MSFTNGP10.phx.gbl#link1 ...
4
by: WaterWalk | last post by:
Hello, I'm currently learning string manipulation. I'm curious about what is the favored way for string manipulation in C, expecially when strings contain non-ASCII characters. For example, if...
2
by: David Morgan | last post by:
Hi Have 4Gb of RAM and plenty of free disk. Those page faults are for DLLHOST.EXE using ~370Mb RAM. inetinfo.exe has 403,106,036 page faults at the time of writing and is using ~145Mb RAM. ...
0
by: Dale Huffman | last post by:
There have been a number of posts about calling gui methods from other threads. Eric Brunel. has reccommended calling the gui's ..event_generate method with data passed thru a queue. This worked...
3
by: scotp | last post by:
Does anyone know what would cause excessive page faults running the js function below? The most common browser used is IE 6. The page has records that include text & checkbox inputs. Each...
4
by: none | last post by:
I have an ASP.NET application, hosted on two web servers. I am looking for advice on what should be an acceptable level of page faults on these production servers. If the acceptable level is zero,...
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: 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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.