I have a string S and I want to print all its suffix, I tried the following but I seem to be missing a number of letters; -
char *S;
-
scanf("%s", &S);
-
int l,i;
-
l = strlen(S);
-
for(i = 0;i < l;i++){
-
printf("%s",&S[i]);
-
}
-
When I input a word of length greater the 8, I lose all the letters after the 8th char.
for example -
mississippi //this is the input
-
mississippi
-
ississi
-
ssissi
-
sissi
-
issi
-
ssi
-
si
-
i
-
-
-
-
-
char *S;
-
scanf("%s", &S);
-
This is undefined behaviour, you do not allocate any memory to S and then rather than passing where S points to you pass a pointer to where S is to scanf.
Reading a string should look like this using scanf -
char S[100];
-
scanf("%s", S);
-
But that is insecure as it allows buffer overflow so you should rather do this -
char S[100];
-
fgets(S, sizeof S, stdin);
-
4 7655
I don`t think you need the for loop (l5) or the[i] (l6)
The loop is for-shortening the word.
Banfa 9,065
Expert Mod 8TB -
char *S;
-
scanf("%s", &S);
-
This is undefined behaviour, you do not allocate any memory to S and then rather than passing where S points to you pass a pointer to where S is to scanf.
Reading a string should look like this using scanf -
char S[100];
-
scanf("%s", S);
-
But that is insecure as it allows buffer overflow so you should rather do this -
char S[100];
-
fgets(S, sizeof S, stdin);
-
Thanks but what if I don't know the maximum length of the string?
Banfa 9,065
Expert Mod 8TB
What if you don't know the maximum length of the string being input?
You guess or you specify the maximum your program excepts you do something more intelligent reading a few characters at a time and transfering them to a dynamically allocated buffer (malloc'd) that you can resize if required.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Krakatioison |
last post by:
My sites navigation is like this:
http://www.newsbackup.com/index.php?n=000000000040900000
, depending on the variable "n" (which is always a number), it will take me
anywhere on the site......
|
by: Stu Cazzo |
last post by:
I have the following:
String myStringArray;
String myString = "98 99 100";
I want to split up myString and put it into myStringArray.
If I use this:
myStringArray = myString.split(" ");
it...
|
by: John F Dutcher |
last post by:
I use code like the following to retrieve fields from a form:
recd =
recd.append(string.ljust(form.getfirst("lname",' '),15))
recd.append(string.ljust(form.getfirst("fname",' '),15))
etc.,...
|
by: Derek Hart |
last post by:
I wish to execute code from a string. The string will have a function name,
which will return a string:
Dim a as string
a = "MyFunctionName(param1, param2)"
I have seen a ton of people...
|
by: Angus Leeming |
last post by:
Hello,
Could someone explain to me why the Standard conveners chose to typedef
std::string rather than derive it from std::basic_string<char, ...>?
The result of course is that it is...
|
by: Andrew |
last post by:
I have written two classes : a String Class based on the book " C++ in
21 days " and a GenericIpClass listed below :
file GenericStringClass.h
// Generic String class
|
by: Locusta |
last post by:
Hello,
I have been struggeling for replacing a string in a string. The snippet
from the program below replaces the <, & and > with the XML equivalent
values.
In the program, I allocate space...
|
by: zoro |
last post by:
Hi,
I am new to C#, coming from Delphi. In Delphi, I am using a 3rd party
string handling library that includes some very useful string
functions, in particular I'm interested in BEFORE (return...
|
by: Emilio |
last post by:
Question about
Shared Sub Connect(server As , message As )
Why is in square brackets?
Is it like
Shared Sub Connect(server() As String, message() As String)
|
by: Badass Scotsman |
last post by:
Hello,
Using VB and ASP,NET I would like to be able to search a STRING for a
smaller STRING within, based on the characters which appear before and
after.
For example:
String1 = " That was...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |