473,761 Members | 10,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

More problems with string manipulation and seg faults

20 New Member
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 1571
Banfa
9,065 Recognized Expert Moderator Expert
k is never initialised
Mar 8 '07 #2
Acolyte
20 New Member
Yeah, I noticed that right after I posted. However, I've changed that now, to no avail.
Mar 8 '07 #3
horace1
1,510 Recognized Expert Top Contributor
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
1748
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 to dblocal and attempt to connect, using a seperate connection. Once I attempt to open this second connection, an exception is thrown. I used the connection string built by the server explorer for both databases so the problem should not be...
12
1846
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 then remove certain stopwords(like and, a, an, by, the etc) which is there in another txt file. Then, i need to run it thru a stemmer(a program which converts words like running to run, ie, converts them to roots words).
4
3804
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, Graphics.FillRectangle causes a lot of soft page faults - order of 700/sec and more... When Control.DoubleBuffer=false, we have no page faults at all - 0/sec. Has anyone seen this behavior and how did they resolve it...? What are our options...? Does...
2
2292
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 In the current release of our system, we decided to "wrap" the ASP.NET Session and Application objects to improve code clarity and accuracy. For example, instead of: Session = new SomeListClass;
4
3491
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 substrings need be replaced, or one character needs be changed, what shall I do? Is it better to convert strings to UCS-32 before manipulation? But on Windows, wchar_t is 16 bits which isn't enough for characters which can't be simply encoded...
2
4157
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. Why so many page faults. System uptime ~500 hours. The ASP-based site does interface with a SQL Server instance on a completely
0
2205
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 great for me until trying to write to the gui from multiple threads. There I had problems: random typesof crashes almost all resulting in seg faults. I thought this info might help anyone trying to do the sameor at least save some time...
3
1830
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 record also has a hidden input named "questions", whose value is an id that is used in the name of inputs to be disabled. The number of page faults increases dramatically as records increase. I know it is the for loop that is taking the extra time,...
4
2169
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, then where should I begin reducing page faults in code? What should I look for in code to reduce page faults? Having to reduce page faults is something I have not had been tasked with before, so I am not sure what is acceptable and where to...
0
9333
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10107
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9945
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9900
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8768
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7324
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5214
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.